标签:
1、输入和输出对象
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>表单提交</title> </head> <body> <form id="form1" name="form1" method="post" action="test"> <p align="center"><strong>表单提交</strong></p> <table width="331" height="147" border="1" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="76" height="35">username:</td> <td width="183"> <label> <input type="text" name="userName" id="username_tf" height="20"/> </label> </td> <td width="50"> </td> </tr> <tr> <td>sex:</td> <td> <input type="radio" name="sex" value="boy"/>Boy <input type="radio" name="sex" value="girl"/>Girl </td> <td> </td> </tr> <tr> <td>address:</td> <td> <input type="text" name="address" id="address_tf" height="20"/> </td> <td> </td> </tr> <tr> <td>likes:</td> <td> <label> <input type="checkbox" name="likes" id="cb1" value="sing"/>Sing <input type="checkbox" name="likes" id="cb2" value="dance">Dance <input type="checkbox" name="likes" id="cb3" value="game">Game </label> </td> <td> </td> </tr> <tr> <td> </td> <td> <input name="" type="submit" value="提交"/> <input name="" type="reset" value="重置"> </td> <td> </td> </tr> </table> </form> </body> </html>
2)在Servlet中处理业务逻辑
package com.yyq.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * Created by gao on 16-4-18. */ public class TestServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取表单信息 String userName = req.getParameter("userName"); String sex = req.getParameter("sex"); String address = req.getParameter("address"); String[] likes = req.getParameterValues("likes"); String URL = "index.jsp"; String likes2 = ""; if (null != likes) { for (String str : likes) { likes2 += str + " "; } } if (null != userName && !userName.equals("")) { //把从页面获取的内容放入到request中 req.setAttribute("userName", userName); req.setAttribute("sex", sex); req.setAttribute("address", address); req.setAttribute("likes", likes2); URL = "result.jsp"; } req.getRequestDispatcher(URL).forward(req, resp); } @Override public void destroy() { super.destroy(); } @Override public void init() throws ServletException { super.init(); } }
3)在web.xml文件中注册Servlet信息
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" 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"> <servlet> <servlet-name>testservlet</servlet-name> <servlet-class>com.yyq.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>
4)编写结果页面result.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>结果页面</title> </head> <body> <form id = "form" name="form" method="post" action=""> <p align = "center"><strong>表单 提交</strong></p> <table width = "331" height = "147" border="1" align = "center" cellpadding = "0" cellspacing = "0"> <tr> <td width = "76" height = "35">username:</td> <td width = "183"> <label><%=request.getAttribute("userName")%></label> </td> <td width = "50"> </td> </tr> <tr> <td> Sex:</td> <td><%=request.getAttribute("sex")%></td> <td> </td> </tr> <tr> <td> Address:</td> <td><%=request.getAttribute("address")%></td> <td> </td> </tr> <tr> <td> likes:</td> <td> <label><%=request.getAttribute("likes")%></label> </td> <td> </td> </tr> <tr> <td> </td> <td> <a href="index.jsp">Back index.jsp<br/></a> </td> <td> </td> </tr> </table> </form> </body> </html>
5)启动Tomcat,输入http://localhost:8080/index.jsp
//JSP引擎会自动把输出的页面转换成指定的字符集 resp.setContentType("text/html;charset=GB2312"); //在JSP中,可以使用req.getParameter("参数名")获得参数值, //参数值的默认字符集是ISO8859_1,如果不进行字符集转换,将导致汉字乱码 String szUserName = req.getParameter("userName"); szUserName = new String(szUserName.getBytes("ISO8859_1"),"GB2312");
package com.yyq.servlet; import javax.servlet.*; import java.io.IOException; /** * Created by gao on 16-4-25. */ public class FormFilter implements Filter { private FilterConfig filterConfig; //Filter初始化时的回调方法 //FilterConfig接口实例中封装了这个Filter的初始化参数 @Override public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; } //主要负责过滤工作 //FilterChain用于调用过滤器链中的下一个顾虑器 @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { //获取此Filter的初始化参数的值 String encoding = filterConfig.getInitParameter("encoding"); System.out.println(encoding); if (null != encoding && !"".equals(encoding)){ //设置请求数据的编码方式 servletRequest.setCharacterEncoding(encoding); } //把请求和响应对象传给过滤器链中的下一个要调用的过滤器或Servlet filterChain.doFilter(servletRequest,servletResponse); } //此Filter被释放时的回调方法 @Override public void destroy() { //空 } }
修改配置文件web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" 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"> <servlet> <servlet-name>testservlet</servlet-name> <servlet-class>com.yyq.servlet.TestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>testservlet</servlet-name> <url-pattern>/test</url-pattern> </servlet-mapping> <filter> <filter-name>FormFilter</filter-name> <filter-class>com.yyq.servlet.FormFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>FormFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
输出汉字:
标签:
原文地址:http://www.cnblogs.com/yangyquin/p/5431769.html