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)
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: <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> I hope you have understood the code.
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
thx for the help but still iam having errors i have edited my code as follows : 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.handleJspException(JspServletWrapper.java:505) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) root cause java.lang.NumberFormatException: For input string: "n" java.lang.NumberFormatException.forInputString(Unknown Source) java.lang.Integer.parseInt(Unknown Source) java.lang.Integer.parseInt(Unknown Source) org.apache.jsp.check_jsp._jspService(check_jsp.java:66) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
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>
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..!!:charming:
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.
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
thanx a lot for ur feedback regarding my blog...i m a part time blogger for now...not getting enough time to write..MY old blog has some unique contents from which only i m getting the visitors all over the world and thatz why i purchased a domain so that i can move forward with some advanced stuffs....i have visited ur blog as well..itz nice..i m astounded why r u not getting traffic?? keep working man nd keep ur blog filled with unique contents no matter how many posts u have. I dnt have much posts but for 3-4 posts i come in first page in google search. Keep working on it!! best of luck!