Iam new to jsp and trying to get input from user and then check either the input match with the number provided by the programmer. here is my code :
<form method="get">
Guess number :
<input type="text" name="n" />
<%
String a=63;
String n = request.getParameter("n");
int g=Integer.parseInt("n");
if(g==a)
{
out.println("you won");
}
else
{
out.println("wrong guess");
}
%>
</form>
but i am having errors which are as follows :
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 17 in the jsp file: /guess number.jsp
Type mismatch: cannot convert from int to String
14: <input type="text" name="n" />
15:
16: <%
17: String a=63;
18: String n = request.getParameter("n");
19: int g=Integer.parseInt("n");
20: if(g==a)
An error occurred at line: 20 in the jsp file: /guess number.jsp
Incompatible operand types int and String
17: String a=63;
18: String n = request.getParameter("n");
19: int g=Integer.parseInt("n");
20: if(g==a)
21: {
22: out.println("you won");
23: }
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.jav acError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacEr ror(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateCla ss(JDTCompiler.java:439)
org.apache.jasper.compiler.Compiler.compile(Compil er.java:334)
org.apache.jasper.compiler.Compiler.compile(Compil er.java:312)
org.apache.jasper.compiler.Compiler.compile(Compil er.java:299)
org.apache.jasper.JspCompilationContext.compile(Js pCompilationContext.java:586)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
|
Skilled contributor
|
![]() |
| 20Dec2009,16:32 | #2 |
|
Hi tahir,
the way u developed ur code clearly indicates that u r new to JSP. Actually, u dnt have the clear idea about the get and post commands. When u use "get" or "Post" command, it indicates that the parameters are to be diverted to the some other page i.e in ur case the number which u r taking as an input from a end user must be carried to some other page nd in the other page the logic must be applied with the received input. Let me explain u. Make one .html file and a .jsp file. Name of the html file wud be number.html nd that of the .jsp file be check.html Now the snippet for html file wud be:- HTML Code:
<html> <body> <form method="get" action="check.jsp"> <input type="text" name="n" /> <input type="submit"/> </form> </body> </html> NOw the snippet for the jsp page wud be:- <html> <body> <% String a=63; String n = request.getParameter("n"); int g=Integer.parseInt("n"); if(g==a) { out.println("you won"); } else { out.println("wrong guess"); } %> </body> </html> |
|
Skilled contributor
|
![]() |
| 20Dec2009,16:36 | #3 |
|
sorry!! u did one more thing wrong over dr...instead of
String a=63 u need to write int a=63.. i overlooked that..sorry one again |
|
Light Poster
|
|
| 20Dec2009,19:39 | #4 |
|
Quote:
Originally Posted by techgeek.in number : <form method="get" action="check.jsp"> <input type="text" name="n" /> <input type="submit"/> </form> check : <body> <% int a=63; String n = request.getParameter("n"); int g=Integer.parseInt("n"); if(g==a) { out.println("you won"); } else { out.println("wrong guess"); } %> </body> now the errors are as follows : type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception org.apache.jasper.JasperException: An exception occurred processing JSP page /check.jsp at line 13 10: <% 11: int a=63; 12: String n = request.getParameter("n"); 13: int g=Integer.parseInt("n"); 14: 15: if(g==a) 16: { Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handle JspException(JspServletWrapper.java:505) org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:416) org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:342) org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:267) javax.servlet.http.HttpServlet.service(HttpServlet .java:717) root cause java.lang.NumberFormatException: For input string: "n" java.lang.NumberFormatException.forInputString(Unk nown Source) java.lang.Integer.parseInt(Unknown Source) java.lang.Integer.parseInt(Unknown Source) org.apache.jsp.check_jsp._jspService(check_jsp.jav a:66) org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet .java:717) org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:374) org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:342) org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:267) javax.servlet.http.HttpServlet.service(HttpServlet .java:717) |
|
Skilled contributor
|
![]() |
| 20Dec2009,20:02 | #5 |
|
use the following code for check.jsp. I have verified it myself.
<html> <body> <% int a=63; int g=Integer.parseInt(request.getParameter("n")); if(g==a) { out.println("you won"); } else { out.println("wrong guess"); } %> </body> </html> |
|
Light Poster
|
|
| 20Dec2009,20:13 | #6 |
|
thx friend thank you very much it really works and also helps me to understand the syntax and helped to improve my concept thank you once again..!!
|
|
Skilled contributor
|
![]() |
| 20Dec2009,20:28 | #7 |
|
netime... techgeek.in is always on...
|
|
Ambitious contributor
|
![]() |
| 21Dec2009,07:44 | #8 |
|
The reason for getting the error the second time(line 13) is that the Integer.parseInt() needs a number string. But what tahir used was Integer.parseInt("n") instead of using like this: Integer.parseInt(n).
So, Integer.parseInt("n") will take the value n as parameter, and, Integer.parseInt(n) will take the value 63 as parameter. |
|
Skilled contributor
|
![]() |
| 21Dec2009,08:48 | #9 |
|
yah..u r correct..i have corrected that only when second time i posted the code for check.jsp
|
|
Ambitious contributor
|
![]() |
| 21Dec2009,23:53 | #10 |
|
Yes I could understand that from your two posts instead of a single post. In order for tahir to understand the reason for the error, I posted with a little bit of explanation
|

