Note that there are some explanatory texts on larger screens.

plurals
  1. PONumber Format Exception in Java Servlet
    primarykey
    data
    text
    <p>I have made a simple servlet in Eclipse running on Tomcat server.</p> <p>I am running a simple java application which passes a string <code>message=20</code> to the servlet. The servlet gets the data. I am using </p> <pre><code>String name= request.getParameter("message"); </code></pre> <p>to get the passed string. When I return the name value back to the application, I am able to get it back. But when I try to process the string name:</p> <pre><code>int val=Integer.parseInt(name); </code></pre> <p>I get <code>NumberFormatException</code>. I cannot figure out what the problem is. I have been able to get the value of name variable back from the servlet, but why can't I use it?</p> <p>The Code I am using is as follows:</p> <p><strong>CLIENT SIDE CODE</strong></p> <pre><code>URL url = new URL("http://localhost:8080/DemoServer/demoservelet"); URLConnection conn = url.openConnection(); conn.setDoOutput(true); BufferedWriter out = new BufferedWriter( new OutputStreamWriter( conn.getOutputStream() ) ); out.write("message=20"); </code></pre> <p><strong>SERVLET CODE</strong></p> <pre><code>String username = request.getParameter("message").toString(); int val=Integer.parseInt(username); //get error msg on this line response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("Passed val :"+ username+username.length()); } </code></pre> <p>I am getting error on the conversion. One more thing on return I am getting a length value of 4 which also does not make any sense?</p> <p>Please help me regarding this. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload