Servlet Small Example: JSP page submission information servlet receives and prints output

Source: Internet
Author: User

The example submits the form using the Dopost method, which contains a total of two files.

One is the form userform2.jsp used to submit user information, and the other is the servlet used to receive the parameters.

userform2.jsp

1<%@ page language= "java" contenttype= "text/html;charset=gb2312"%>2 345   6<body>7<form action= "Dopostdemo" method= "POST" >8User name: <input type= "text" name= "username" ><br/>9Password: <input type= "password" name= "password" ><br/>Ten<input type= "Submit" value= "Submission" > One<input type= "reset" value= "reset" > A</form> -</body> -

Servlet that receives parameters

1  PackageCOM.MHB;2 3 Importjava.io.IOException;4 ImportJava.io.PrintWriter;5 6 Importjavax.servlet.ServletException;7 ImportJavax.servlet.http.HttpServlet;8 Importjavax.servlet.http.HttpServletRequest;9 ImportJavax.servlet.http.HttpServletResponse;Ten  One  Public classDopostdemoextendsHttpServlet { A  Public voidInit ()throwsservletexception { - } -  Public voidDoPost (httpservletrequest request, httpservletresponse response) the throwsservletexception, IOException { -Response.setcontenttype ("text/html;charset=gb2312"); -PrintWriter out =Response.getwriter (); -  + //set the encoding format for receive parameters -Request.setcharacterencoding ("gb2312"); + //get Username,password parameter AString username = request.getparameter ("username"); atString Password = request.getparameter ("Password"); -  -Out.println ("<HTML>"); -Out.println ("<BODY>"); -Out.print ("User name:" +username+ "<br/>"); -Out.print ("Password:" +password+ "<br/>"); inOut.println ("</BODY>"); -Out.println ("</HTML>"); to } +  Public voiddestroy () { - Super. Destroy (); the } *}

Web. XML configuration

  <servlet>    <servlet-name>DoPostDemo</servlet-name>    <servlet-class> com.mhb.dopostdemo</servlet-class>  </servlet>  <servlet-mapping>    < servlet-name>dopostdemo</servlet-name>    <url-pattern>/DoPostDemo</url-pattern>  </servlet-mapping>

Browser display:

Servlet Small Example: JSP page submission information servlet receives and prints output

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.