Servlet Program Development--servlet and forms

Source: Internet
Author: User

Servlet Program:

The Dopost method is used in order to prevent a form from being submitted when the Post method is in question. Otherwise, only get requests can be processed

 PackageOrg.lxh.servletdemo;ImportJava.io.* ;Importjavax.servlet.* ;Importjavax.servlet.http.* ; Public classInputservletextendshttpservlet{ Public voiddoget (httpservletrequest req,httpservletresponse resp)throwsservletexception,ioexception{String Info= Req.getparameter ("info");//assuming the parameter name is infoPrintWriter out =Resp.getwriter (); Out.println (") ; Out.println (") ; Out.println ("<body>") ; Out.println (") ; Out.println ("</body>") ; Out.println (") ;    Out.close (); }     Public voiddoPost (httpservletrequest req,httpservletresponse resp)throwsservletexception,ioexception{ This. Doget (REQ,RESP); }}

Web. XML configuration:

Add this statement: note the following path issues, because input.htm is under the Inputservlet folder.

<servlet>        <servlet-name>input</servlet-name>        <servlet-class> org.lxh.servletdemo.inputservlet</servlet-class>    </servlet>    <servlet-mapping >        <servlet-name>input</servlet-name>        <url-pattern>/inputservlet/inputservlet </url-pattern>    </servlet-mapping>

Then create the form: The form path is under the Inputservlet folder

<HTML><HEAD><TITLE>WWW.MLDNJAVA.CN,MLDN High-end Java training </title>    Please enter content:<input type= "text" name= "info" >    < Input type= "Submit" value= "Submission" ></form></body>

The above approach to setting the path in Web. XML is to avoid development. /the problem. Otherwise, you will need to use this statement in the form file above:

<form action= ". /inputservlet "method=" POST ">

Summarize:

1. Different request modes when forms are received: doget,dopost

2. Be sure to handle the routing problem when submitting a form



Servlet Program Development--servlet and forms

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.