JSP ServletParameter Transfer Between Form and Servlet
//index.html
Then passparams the Servlet's doget/dopost to handle the parameter param1-3
package myServlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.PrintWriter;public class PassParams extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {PrintWriter out = resp.getWriter();out.write("doGet\r\n");out.write(req.getParameter("param1"));out.write("\r\n");out.write(req.getParameter("param2"));out.write("\r\n");out.write(req.getParameter("param3"));out.write("\r\n");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// TODO Auto-generated method stubdoGet(req,resp);}/** * */private static final long serialVersionUID = 1L;}
/////////////////////////////
That is, HTML submits the form in post mode, and passparams receives and processes the parameters ..
<Form name ="Myform"Method ="Post"Action ="Myservlet/passparams">
The parameter action indicates
Action |
URL |
Specifies where to send form data when a form is submitted. |
Method |
·Get ·Post |
Specifies how to send form data. |
There will be a lot of post requests, and no form data will be displayed on the URL, and get will ....
<Input name ="Param1"Type ="Text"/> Indicates
Name |
Field_name |
Defines the name of the input element. |
In servlet, req. getparameter ("param1") obtains the parameter param1 value in servlet...
Add the following content to Web. xml:
<servlet> <servlet-name>PassParams</servlet-name> <servlet-class>myServlet.PassParams</servlet-class> </servlet> <servlet-mapping> <servlet-name>PassParams</servlet-name> <url-pattern>/myServlet/PassParams</url-pattern> </servlet-mapping>
//////////////////////////////////////// /////////////////////////////////////
<URL-pattern>/myservlet/passparams </url-pattern>
Indicates the path to access this servlet .... Eventually:
Passparams dopost access path:
HTTP: /localhost: 8080/passparams/myservlet/passparams
Example:
Http: // localhost: 8080/passparams/index.html
Result: