By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,955 Members | 1,768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,955 IT Pros & Developers. It's quick & easy.

How pass a form parameter value to a same jsp file?

P: n/a
Hi,
I have a following jsp file called "showAsset.jsp" :

<form action="showAsset.jsp" method="post">
<tr BGCOLOR=YELLOW>

<td><input name="sort" type="submit" value="Asset_Number" > </td>
<td><input name="sort" type="submit" value="Description"></td>
<td><input name="sort" type="submit" value="Quantity"></td>
<td><input name="sort" type="submit" value="Sub System"></td>
<td><input name="sort" type="submit" value="Model"></td>

</tr>
</form>

<% String sort1=request.getParameter("sort");
String i=" ";
if (sort1.equals("Asset_Number"))
i="Asset_Number";
else if(sort1.equals("Description"))
i="Description";
else
i="Type";
%>

<%=i%>

But when i put it in Tomcat5.0 server, It give me the following error
message:
-------------------------------------------------------------------------------
HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented
it from fulfilling this request.

exception

org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:358)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet .java:856)
root cause

java.lang.NullPointerException
org.apache.jsp.testdb.showAsset1_jsp._jspService(s howAsset1_jsp.java:61)
org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet .java:856)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet .java:856)
-------------------------------------------------------------------------------

My question is that how can i pass a form value to a same jsp file?

Thank you

Ricky
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi Ricky,
I think that you doing it right except that: When you first load the
jsp, this code request.getParameter("sort"); whill return null because
at the first load you do not have sort parameter. So sort1=null.
Because sort1=null sort1.equals("Asset_Number") will return
NullPointerException.

You should do this:
1)First way - to check if sort1==null

<% String sort1=request.getParameter("sort");
String i=" ";
if(sort1!=null){ // you should check if sort == null
if (sort1.equals("Asset_Number"))
i="Asset_Number";
else if(sort1.equals("Description"))
i="Description";
else
i="Type";
}else{
i="Type";
}
%>

2)Second way use "Some string".equals(sort1)- if sort1==null no
Exception trown
<% String sort1=request.getParameter("sort");
String i=" ";
if ("Asset_Number".equals(sort1)) i="Asset_Number";
else if("Description".equals(sort1)) i="Description";
else
i="Type";
%>

I think that both ways solve your problem.
Zhivko Mitrev
Jul 17 '05 #2

P: n/a
Hi Ricky,
I think that you doing it right except that: When you first load the
jsp, this code request.getParameter("sort"); whill return null because
at the first load you do not have sort parameter. So sort1=null.
Because sort1=null sort1.equals("Asset_Number") will return
NullPointerException.

You should do this:
1)First way - to check if sort1==null

<% String sort1=request.getParameter("sort");
String i=" ";
if(sort1!=null){ // you should check if sort == null
if (sort1.equals("Asset_Number"))
i="Asset_Number";
else if(sort1.equals("Description"))
i="Description";
else
i="Type";
}else{
i="Type";
}
%>

2)Second way use "Some string".equals(sort1)- if sort1==null no
Exception trown
<% String sort1=request.getParameter("sort");
String i=" ";
if ("Asset_Number".equals(sort1)) i="Asset_Number";
else if("Description".equals(sort1)) i="Description";
else
i="Type";
%>

I think that both ways solve your problem.
Zhivko Mitrev
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.