Self-understanding of JSP application Three: Jsp+servlet implementation form Validation

Source: Internet
Author: User

Example description: Through a form submission page submitted to Servlet,servlet to obtain information, after processing, the information into the request object, if the user submits the name is empty, will return to the home page, otherwise, the submission of all the information displayed

The following specific implementation ~ ~

1, a new Dynamic Web project, named FORMTT



2, a new login interface index.jsp, complete the written login interface, the specific code is as follows:

<%@ page language= "java" contenttype= "text/html; CHARSET=GBK "pageencoding=" GBK "%> <!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > The specific interface is as follows:



3, the server side to build a package org.luojs.servlet, and then under the package of a new Testservlet Java class, complete server-side processing of the form

The specific code is as follows:

Package org.luojs.servlet;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;
	public class Testservlet extends httpservlet{public void Destroy () {Super.destroy ();
		public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
	This.dopost (request, response); 
		public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
		Gets the form information String name = Request.getparameter ("name");
		String sex = request.getparameter ("Sex");
		String address = Request.getparameter ("Address");
		string[] likes = request.getparametervalues ("likes");
		String URL = "index.jsp";
		String likes2 = "";
			if (null!= likes) {for (String string:likes) {likes2 + = string + ""; } if (null!= name &&!name.equals ("")) {
			Put the content you get from the page into the request Request.setattribute ("name", name);
			Request.setattribute ("Sex", sex);
			Request.setattribute ("Address", address);
			Request.setattribute ("likes", Likes2);
		URL = "result.jsp";
	} request.getrequestdispatcher (URL). Forward (request, response); public void Init () throws servletexception {}}

4. Configure Web.xml Files

<?xml version= "1.0" encoding= "UTF-8"?> <web-app "xmlns="
  xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee
                      Http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "
  version=" 3.0 "
  metadata-complete=" true ">
  
  < !--Register servlet information-->
  <servlet>
  	<servlet-name>TestServlet</servlet-name>
  	< servlet-class>org.luojs.servlet.testservlet</servlet-class>
  </servlet>
  < servlet-mapping>
  	<servlet-name>TestServlet</servlet-name>
  	<url-pattern>/test </url-pattern>
  </servlet-mapping>
  <welcome-file-list>
  	<welcome-file> index.jsp</welcome-file>
  </welcome-file-list>

 </web-app>

5, run a run to try, found also OK,


But when you enter Chinese text, there are garbled characters.



Therefore, you need to use the filter listener technology to achieve the problem of Chinese garbled ~ ~

(1) Under the Org.luojs.servlet package, create a new charaterencoding Java class, the specific code is as follows:

Package org.luojs.servlet;

Import java.io.IOException;
Import Javax.servlet.Filter;
Import Javax.servlet.FilterChain;
Import Javax.servlet.FilterConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletRequest;

Import Javax.servlet.ServletResponse;

	public class Characterencoding implements filter{private Filterconfig config; The callback method public void Destroy () {}////filterchain is used to invoke the next filter public void Dofilter in the filter chain when this filter is released (SERVLETR Equest request, servletresponse response, Filterchain chain) throws IOException, servletexception {//Get the initial parameter of this filter
		
		Value String encoding = config.getinitparameter ("encoding"); if (null!= encoding &&! "".  Equals (encoding)) {request.setcharacterencoding (encoding);
	Set the encoding of the request data}//Send the request and response object to the next filter or Sevlet Chain.dofilter in the filter chain (request, response); }//Some of the callback methods for filter initialization//filterconfig The initialization parameters of this filter in the interface instance public void init (Filterconfig config) throws Servletexcepti on {THIS.CONFIg = config;
 }	

}

(2) Add the following code to the Web.xml:

  <!--solve Chinese garbled problem-->
  <filter>
  	<filter-name>CharacterEncoding</filter-name>
  	< filter-class>org.luojs.servlet.characterencoding</filter-class>
  	<init-param>
  	< param-name>encoding</param-name>
  	<param-value>GBK</param-value>
  	</init-param >
  </filter>
<filter-mapping>
	<filter-name>characterencoding</filter-name >
	<url-pattern>/*</url-pattern>
</filter-mapping>

Then, you run, you see the Chinese characters, almost ~ ~



Bibliography: "JSP Basics and Case development detailed", Tsinghua University Press

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.