Wrox Home  
Search P2P Archive for: Go

  Return to Index  

servlets thread: why Servlet can't receive right result from JSP?


Message #1 by Edward <zhangsc@n...> on Fri, 14 Jun 2002 15:24:03 +0800
Edward
The problem is with your javascript:
   <form  method="get" >
     <input type="submit" name="Open" value="Open" style="width:200px"
onClick="this.form.action='/test/servlet1?value=99&msg=Open&kubun=1'">
     <input type="submit" name="Private" value="Private" style="width:200px"
onClick="this.form.action='/test/servlet1?value=18&msg=Private&kubun=0'">
</form>

That is the output.  The "this.form.action" is the problem.   This takes the
URL for the form submission ('/test/servlet1?value=18&msg=Private&kubun=0').
You are trying to include the parameters in the URL - a reasonable try but
the browser does not accept this and truncates the URL to a valid URL - ie
/test/servlet1 - and your extra parameters are lost.

If you need to do this I suggest placing the the values into three hidden
parameters (value, msg and kubun).  You can then, using javascript update
their values using Form.elements[] array to extract the Hidden javascript
objects and update the value with your desired values before submitting.

Hope this helps,
Andrew
Professional Java Servlets 2.3
http://www.amazon.com/exec/obidos/ASIN/186100561X
http://www.amazon.co.uk/exec/obidos/ASIN/186100561X

-----Original Message-----
From: Edward [mailto:zhangsc@n...]
Sent: 14 June 2002 08:24
To: Servlets
Subject: [servlets] why Servlet can't receive right result from JSP?

I want to make Servlet receive message from JSP.But Servlet show message is
null,I'm very puzzled with it.My files is follows:

//Jsp.jsp
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
</head>
<body>
    <%
      String message[]=new String[2];
      String SysID[]=new String[2];
      String kubun[]=new String[2];
      message[0]="Open";
      message[1]="Private";
      SysID[0]="99";
      SysID[1]="18";
      kubun[0]="1";
      kubun[1]="0";
   %>
   <form  method="get" >
   <%
      for(int cntr=0;cntr<2;cntr++)
      {
   %>
     <input type="submit" name="<%= message[cntr] %>" value="<%
message[cntr] %>" style="width:200px"
onClick="this.form.action='/test/servlet1?value=<%= SysID[cntr] %>&msg=<%
message[cntr] %>&kubun=<%= kubun[cntr] %>'">
    <%
       }
    %>
</form>
</body>
</html>

//Servlet1.java
package testjsp;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class Servlet1 extends HttpServlet {
  private static final String CONTENT_TYPE = "text/html; charset=GBK";
  //Initialize global variables
  public void init() throws ServletException {
  }
  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
    response.setContentType(CONTENT_TYPE);
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head><title>Servlet1</title></head>");
    out.println("<body>");
    out.println("<p>The servlet has received a GET. This is the
reply.</p>");
    out.println("<p>kubun is "+request.getParameter("kubun")+"</p>");
    out.println("<p>Message is "+request.getParameter("msg")+"</p>");
    out.println("<p>SysID is "+request.getParmeter("SysID")+"</p>");
    out.println("</body></html>");
  }
  //Clean up resources
  public void destroy() {
  }
}

I have thought it for a long time,but I still understand why Servlet can't
receive right result.Why I can't get the result of SysID,kubun and msg?
Any idea will be appreciated!
Thanks!!!
Edward



  Return to Index