Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By gimbal2

Thread: Not getting parameter in Servlet

  1. #1
    handro1104 is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default Not getting parameter in Servlet

    Java Code:
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		String xmlParameter = "xml";
    		String xmlString = request.getParameter(xmlParameter);
    		int length = xmlString.length();
    		xmlString = xmlString.substring(1, length - 1);
    		xmlString = xmlParse(xmlString);
    		String xmlResponse = "<!DOCTYPE html>" + "<html>" + "<body>" + "<h1>POSREQUEST</h1>";
    		xmlResponse += xmlString;
    		xmlResponse += "</body>" + "</html>";
    		response.setContentType("text/html");
    		PrintWriter out = null;
    		try {
    			out = response.getWriter();
    			out.println(xmlResponse);
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			out.flush();
    			out.close();
    		}
    	}
    I am using "http://localhost:8080/MenuTab/servlet/MenuTab?xml=foo", but xmlString is coming empty.
    Last edited by Tolls; 10-31-2013 at 10:18 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,751
    Rep Power
    19

    Default Re: Not getting parameter in Servlet

    When posting code please wrap it in [code] tags [/code] so it retains its formatting. Unformatted code is hard to read.

    Are you sure this code is being executed?
    That's the first question.

    Second, do you mean empty as in "", or null?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    handro1104 is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default Re: Not getting parameter in Servlet

    Quote Originally Posted by Tolls View Post
    When posting code please wrap it in [code] tags [/code] so it retains its formatting. Unformatted code is hard to read.

    Are you sure this code is being executed?
    That's the first question.

    Second, do you mean empty as in "", or null?
    Java Code:
    	public static void main(String[] args) {
    		try {
    			System.out.println("DiveMenu");
    			String xmlString = "";
    			FileReader fileReader = new FileReader("C:\\MyWorkspaces\\Eclipse\\MenuTab\\POSREQUEST.xml");
    			BufferedReader bufferReader = new BufferedReader(fileReader);
    			String line;
    			while ((line = bufferReader.readLine()) != null) {
    				xmlString += line;
    			}
    			xmlString = "\"" + xmlString + "\"";
    //			String url = "http://localhost:8080/MenuTab/servlet/CopyOfS?xml=" + xmlString;
    			String url = "http://localhost:8080/Web/servlet/CopyOfS?xml=" + xmlString;
    			URL obj = new URL(url);
    			HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    			con.setRequestMethod("POST");
    			// xmlString = "foo";
    //			con.addRequestProperty("xml", xmlString);
    			int responseCode = con.getResponseCode();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    Sorry, I am a newbie; I didn't know I could use [code] tags [[B]. I just enclosed the code in code, but it doesn't appear to improve; other forums have tags that you can select with a click, but i didn't find any here.
    Yes, the code runs correctly and the servlet gets the request. By empty I mean ""; that indicates that that the request had a parameter 'xml' but the value received was "".
    Last edited by Tolls; 10-31-2013 at 10:18 AM.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,590
    Rep Power
    5

    Default Re: Not getting parameter in Servlet

    The button is in the advanced editor.

    And prove it that the string is empty when you receive it. See, you also have code that manipulates the value you get. log out the value right after you fetch it and before you do anything with it yourself to prove what you say is true. I don't believe it is.
    Tolls likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 13
    Last Post: 07-25-2013, 05:30 PM
  2. Replies: 1
    Last Post: 08-23-2011, 11:12 AM
  3. Replies: 1
    Last Post: 03-11-2011, 01:55 PM
  4. Replies: 1
    Last Post: 12-02-2008, 12:23 AM
  5. Replies: 1
    Last Post: 08-07-2007, 03:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •