Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Output in a scriptlet method  RSS feed

 
Darko Markovic
Greenhorn
Posts: 16
C++ Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this might be a common thread, but nothing seems to work for me, so I must be doing something wrong! This is the code snippet that's causing me a headache:



I'm extremely new to JSP and I'm wondering why this isn't working! I even had to import the OutputStream class and still, nothing! Any help would be greatly appreciated.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36463
464
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darko,
Welcome to CodeRanch!

What are you expecting to happen? o is null since it is never instantiated. Which means that method will always throw a null pointer. I can't tell where you are trying to write to so it's hard to know what to suggest. A JSP should only write to the JSP/HTML though in which case this method wouldn't be needed at all.

Also, note that the scriptlets have been considered poor practice for JSPs in more than a decade. You might want to use a different book so you learn the current procedures.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are new to JSP, you are heading in the wrong direction, and quickly!

As Jeanne has pointed out, scriptlets in a JSP have been obsolete and disparaged for over 12 years now. 12 years! There is no reason or valid excuse to be writing new code with JSPs that contain scriptlets.

You should be sure that whatever reference you use emphasizes JSPs that use the JSTL (JSP Standard Tag Library) and EL (Expression Language).

Otherwise, you are learning obsolete technology, and developing bad habits that will hard to break later.

I also recommend that newcomers to JSP read the following articles:
  • The Secret Life of JSPs
  • The Front Man

  •  
    Darko Markovic
    Greenhorn
    Posts: 16
    C++ Java Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you Bear and Jeanne! Are there any tutorials you know for modern JSP? So I shouldn't be using <%.......%>? Anyway, what I wanted to do is directly write in the HTML. For some reason, when I write out.println() in a method, it doesn't work, so I'm guessing it's because the class is only looking for its components, which means I have to define the class somehow. I tried doing something like that, but it didn't work, because as you said, the method will always throw a null pointer. To clarify my question, how should I write a string in the HTML? And how could I take information from, for example, a drop down box or a text box? That's what I want to write.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65833
    134
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Darko Markovic wrote:So I shouldn't be using <%.......%>?

    You should not.

    And how could I take information from, for example, a drop down box or a text box? That's what I want to write.


    That's the result of a previous form submission? The request params are available in a builtin EL variable named params. So if the form that was submitted has a text field with a name of "fred", the EL expression to emit its value would be ${params.fred}.

    Search for a tutorial of book that includes JSTL and EL. It's way too vast a subject to write about in a forum post.
     
    Darko Markovic
    Greenhorn
    Posts: 16
    C++ Java Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Bear, will do! I still don't know how to write in the HTML, but I'm glad you told me about how to emit the variable I needed. I'm going to google that as soon as I can! This really is a helpful forum!
     
    Jeanne Boyarsky
    author & internet detective
    Sheriff
    Posts: 36463
    464
    Eclipse IDE Java VI Editor
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Darko Markovic wrote: To clarify my question, how should I write a string in the HTML

    This isn't a good example because it isn't dynamic. You just type the text in your JSP. Special syntax is only needed for the parts that can change at runtime.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65833
    134
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Exactly. For example if you want to put that dynamic parameter in a paragraph, you'd just use:



    You don't have to do anything special to emit the <p> tags. In fact, that's the whole point of a JSP -- anything that's not dynamic is just considered template text, and emitted to the final HTML as is.
     
    Darko Markovic
    Greenhorn
    Posts: 16
    C++ Java Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks again! There must be loads of tags, but I'll try to learn the use of as many basic ones as I can. Your code, Bear, is exactly what I needed.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!