Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP and request variables

 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say I have a jsp page with something like this in it:
<% String page1 = null;
page1 = request.getParameter("page");
if (page1.equals("null"))
page1 = "";
out.println(page1);
%>
<jsp:include page="<%= page1 %>" flush="true" />
In order to get to this page you would have an address like the following: http://127.0.0.1:8080/Operations?page=test.jsp
I want to be able to get to this page like this http://127.0.0.1:8080/Operations without getting a Servlet Exception
SO in a nutshell it my link address contains a ?page=test.jsp then use it but if it does not just load the page still?
Is it possible?
 
PC RE
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check out the servlet specification 2.3 chapter 11.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which can be found here: http://jcp.org/aboutJava/communityprocess/first/jsr053/index.html

But you might be getting a servlet exception because of your code, not because of mappings...

In your code, you retrive the page parameter, and then make an equals(String) comparison. But if you call the page without ?page=test.jsp, this will throw a null pointer exception, which can be thrown by servlet runners as "servlet exception"... look farther down in your stack trace, you may have a 'root cause: java.lang.NullPointerException' And now I've just noticed that you're seeing if it's null by comparing it to "null". You really mean:

Also, you should consider re-writing it to NOT do the include if it's null.


[This message has been edited by Mike Curwen (edited September 10, 2001).]
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hehe,

While checking if there were any replies, I realized that once again, my boolean logic was the exact opposite of my first guess.

It should be && inside the if statement.
 
sea yin
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<% String page1 = "";
page1 = request.getParameter("page");
if (page1.equals(""))
out.println(page1);
%>
<jsp:include page="<%= page1 %>" flush="true" />
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sea:

That will throw a nullpointer exception in some cases.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic