Servlet表单Get和Post读取
新建一个maven的war工程,如果没有web.xml新增一个web.xml或者拷贝一个例如:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> </web-app>
工程代码结构如图所示:
pom.xml添加相关依赖:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>servlet</groupId> <artifactId>servlet</artifactId> <version>1.0.0</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> </dependencies> </project>
一、HelloWorld入门示例
java代码如下:
package servlet.enter; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletEnter extends HttpServlet{ private static final long serialVersionUID = -876565140384532446L; private String message; public void init() throws ServletException{ message="HelloWorld!"; } public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ response.setContentType("text/html"); PrintWriter out=response.getWriter(); out.println("<h1>"+message+"</h1>"); } public void destroy(){ } }
web.xml配置如下:
<servlet> <servlet-name>ServletEnter</servlet-name> <servlet-class>servlet.enter.ServletEnter</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletEnter</servlet-name> <url-pattern>/ServletEnter</url-pattern> </servlet-mapping>
若是报以下错误,多半是Servlet-class的路径没有配对。重新修改下即可。
二、Get方式获取表单数据
java代码如下:
package servlet.form; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/GetFormServlet") public class GetFormServlet extends HttpServlet { private static final long serialVersionUID = 1L; public GetFormServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out=response.getWriter(); String title="使用Get方法读取表单信息"; String name= new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF-8"); String docType="<!DOCTYPE html> \n"; out.println(docType+ "<html>\n" + "<head><title>" + title + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" + " <li><b>站点名</b>:" + name + "\n" + " <li><b>网址</b>:" + request.getParameter("url") + "\n" + "</ul>\n" + "</body></html>"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
可以通过 @WebServlet("/GetFormServlet")来配置Servlet路径,这样就不要在web.xml里头配置Servlet了。
Servlet通过request.getParameter来获取参数,当参数为中文的时候,要注意乱码处理
String name= new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF-8");
Servlet 处理表单数据,这些数据会根据不同的情况使用不同的方法自动解析:
getParameter():您可以调用 request.getParameter() 方法来获取表单参数的值。
getParameterValues():如果参数出现一次以上,则调用该方法,并返回多个值,例如复选框。
getParameterNames():如果您想要得到当前请求中的所有参数的完整列表,则调用该方法。
地址栏输入:
http://localhost:8081/servlet/GetFormServlet?name=shawWey&url=www.shawwey.com
浏览器效果如图:
三、Post方式获取表单数据
java 代码如下:
package servlet.form; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
@WebServlet("/PostFormServlet") public class PostFormServlet extends HttpServlet { private static final long serialVersionUID = 1L; public PostFormServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out=response.getWriter(); String title="使用Post方法读取表单信息"; String name= new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF-8"); String docType="<!DOCTYPE html> \n"; out.println(docType+ "<html>\n" + "<head><title>" + title + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" + " <li><b>站点名</b>:" + name + "\n" + " <li><b>网址</b>:" + request.getParameter("url") + "\n" + "</ul>\n" + "</body></html>"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
页面html代码如下:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>测试教程</title> </head> <body> <form action="PostFormServlet" method="GET"> 网址名:<input type="text" name="name"> <br /> 网址:<input type="text" name="url" /> <input type="submit" value="提交" /> </form> </body> </html>
浏览器效果:
四、另一个表单实例
java代码如下:
package servlet.form; import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Form extends HttpServlet { private static final long serialVersionUID = 1L; public Form() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置响应内容类型 response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String title = "读取所有的表单数据"; String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n"; out.println(docType + "<html>\n" + "<head><meta charset=\"utf-8\"><title>" + title + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<table width=\"100%\" border=\"1\" align=\"center\">\n" + "<tr bgcolor=\"#949494\">\n" + "<th>参数名称</th><th>参数值</th>\n"+ "</tr>\n"); Enumeration paramNames = request.getParameterNames(); while(paramNames.hasMoreElements()) { String paramName = (String)paramNames.nextElement(); out.print("<tr><td>" + paramName + "</td>\n"); String[] paramValues = request.getParameterValues(paramName); // 读取单个值的数据 if (paramValues.length == 1) { String paramValue = paramValues[0]; if (paramValue.length() == 0) out.println("<td><i>没有值</i></td>"); else out.println("<td>" + paramValue + "</td>"); } else { // 读取多个值的数据 out.println("<td><ul>"); for(int i=0; i < paramValues.length; i++) { out.println("<li>" + paramValues[i]); } out.println("</ul></td>"); } out.print("</tr>"); } out.println("\n</table>\n</body></html>"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
配置web.xml
<servlet> <description></description> <display-name>Form</display-name> <servlet-name>Form</servlet-name> <servlet-class>servlet.form.Form</servlet-class> </servlet> <servlet-mapping> <servlet-name>Form</servlet-name> <url-pattern>/Form</url-pattern> </servlet-mapping>
html页面代码如下:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <form action="Form" method="POST" target="_blank"> <input type="checkbox" name="maths" checked="checked" /> 数学 <input type="checkbox" name="physics" /> 物理 <input type="checkbox" name="chemistry" checked="checked" /> 化学 <input type="submit" value="选择学科" /> </form> </body> </html>
本文代码示例: 代码下载