这里模拟了一个注册页面,表单中含有需要需要填写的项目,下面我将实现用request在servlet将前台页面提交给服务器端的数据拿到。
前台页面form.html:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>form.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <form action="/day06/servlet/RequestDemo3" method="post"> 用户名:<input type="text" name="username"><br/> 密码: <input type="password" name="password"><br/> 性别: <input type="radio" name="gender" value="male">男 <input type="radio" name="gender" value="female">女<br/> 所在地: <select name="city"> <option value="beijing">北京</option> <option value="shanghai">上海</option> <option value="changsha">长沙</option> </select><br/> 爱好: <input type="checkbox" name="likes" value="sing"/>唱歌 <input type="checkbox" name="likes" value="dance"/>跳舞 <input type="checkbox" name="likes" value="basketball"/>篮球 <input type="checkbox" name="likes" value="football"/>足球 <br/> 备注:<textarea rows="6" cols="60" name="description"></textarea><br/> 大头照:<input type="file" name="image"/><br/> <input type="hidden" name="id" value="888888"><br/> <input type="submit" name="提交"> </form> </body> </html>
后台servlet
RequestDemo3.java:
package cn.edu.Request; 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 RequestDemo3 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println(request.getParameter("username")); System.out.println(request.getParameter("password")); System.out.println(request.getParameter("gender")); System.out.println(request.getParameter("city")); String likes[]=request.getParameterValues("likes"); for (int i = 0; likes!=null&&i < likes.length; i++) { System.out.println(likes[i]); } System.out.println(request.getParameter("description")); System.out.println(request.getParameter("id")); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }