Parameter Transfer Between Form and Servlet in JSP Servlet

Source: Internet
Author: User

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:

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.