1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'index.jsp' starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 </head> 22 23 <body> 24 <form action="/EasyJsp1.5/servlet/Text" id="form1" name="form1" method="post"> 25 <p align="center"><strong>Message import!</strong></p> 26 <table width="331" height="147" border="1" align="center" cellpadding="0" cellspacing="0"> 27 <tr> 28 <td width="76" height="35"> user name</td> 29 <td width="183"> 30 <label><input type="text" name="name" id="textfield" height="20"/></label> 31 </td> 32 <td width="50"> </td> 33 </tr> 34 <tr> 35 <td>sex: </td> 36 <td> 37 <input type="radio" name="sex" value="boy"/>boy 38 <input type="radio" name="sex" value="girl"/>girl 39 </td> 40 <td width="50"> </td> 41 </tr> 42 <tr> 43 <td>address:</td> 44 <td><input type="text" name="address" id="textfiled3" height="20"/></td> 45 <td width="50"> </td> 46 </tr> 47 <tr> 48 <td>likes: </td> 49 <td><label> 50 <input type="checkbox" name="likes" id="checkbox" value="sing"/>sing 51 <input type="checkbox" name="likes" id="checkbox" value="dance"/>dance 52 <input type="checkbox" name="likes" id="checkbox" value="game"/>game 53 </label></td> 54 <td width="50"> </td> 55 </tr> 56 <tr> 57 <td> </td> 58 <td> 59 <input type="submit" name="" value="submit"/> 60 <input name="" type="reset" value="reset"/> 61 </td> 62 <td> </td> 63 </tr> 64 </table> 65 </form> 66 </body> 67 </html>
1 package servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 public class Text extends HttpServlet { 12 13 /** 14 * Constructor of the object. 15 */ 16 public Text() { 17 super(); 18 } 19 20 /** 21 * Destruction of the servlet. <br> 22 */ 23 public void destroy() { 24 super.destroy(); // Just puts "destroy" string in log 25 // Put your code here 26 } 27 28 /** 29 * The doGet method of the servlet. <br> 30 * 31 * This method is called when a form has its tag value method equals to get. 32 * 33 * @param request the request send by the client to the server 34 * @param response the response send by the server to the client 35 * @throws ServletException if an error occurred 36 * @throws IOException if an error occurred 37 */ 38 public void doGet(HttpServletRequest request, HttpServletResponse response) 39 throws ServletException, IOException { 40 this.doPost(request, response); 41 } 42 43 /** 44 * The doPost method of the servlet. <br> 45 * 46 * This method is called when a form has its tag value method equals to post. 47 * 48 * @param request the request send by the client to the server 49 * @param response the response send by the server to the client 50 * @throws ServletException if an error occurred 51 * @throws IOException if an error occurred 52 */ 53 public void doPost(HttpServletRequest request, HttpServletResponse response) 54 throws ServletException, IOException { 55 PrintWriter out =response.getWriter(); 56 out.println("This is in Text.java!"); 57 String nameString = request.getParameter("name"); 58 String sexString = request.getParameter("sex"); 59 String addString = request.getParameter("address"); 60 String likeString = request.getParameter("likes"); 61 String URL ="/result.jsp"; 62 String likes2 = ""; 63 /** 64 if(null!=likeString) 65 { 66 for(String string : likeString) 67 { 68 likes2 = likes2 + ""; 69 } 70 } 71 */ 72 73 if(null!=nameString && !nameString.equals("")) 74 { 75 request.setAttribute("name", nameString); 76 request.setAttribute("sex", sexString); 77 request.setAttribute("address", addString); 78 request.setAttribute("likes", likeString); 79 //URL="/EasyJsp1.5/result.jsp"; 80 } 81 request.getRequestDispatcher(URL).forward(request, response); 82 } 83 84 /** 85 * Initialization of the servlet. <br> 86 * 87 * @throws ServletException if an error occurs 88 */ 89 public void init() throws ServletException { 90 // Put your code here 91 } 92 93 }
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'result.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 <form action="" id="form1" name="form1" method="post"> 27 <p align="center"><strong>Message expression!</strong></p> 28 <table width="331" height="147" border="1" align="center" cellpadding="0" cellspacing="0"> 29 <tr> 30 <td width="76" height="35"> user name</td> 31 <td width="183"> 32 <label><%=request.getAttribute("name") %></label> 33 </td> 34 <td width="50"> </td> 35 </tr> 36 <tr> 37 <td>sex: </td> 38 <td> 39 <%=request.getAttribute("sex") %> 40 </td> 41 <td width="50"> </td> 42 </tr> 43 <tr> 44 <td>address:</td> 45 <td><%=request.getAttribute("address") %></td> 46 <td width="50"> </td> 47 </tr> 48 <tr> 49 <td>likes: </td> 50 <td><label> 51 <%=request.getAttribute("likes") %> 52 </label></td> 53 <td width="50"> </td> 54 </tr> 55 <tr> 56 <td> </td> 57 <td> 58 <a href="messageInport.jsp">Back import.JSP</a> 59 </td> 60 <td> </td> 61 </tr> 62 </table> 63 </form> 64 </body> 65 </html>
这个是JSP+servlet做页面交互的一个例子,很多东西都用到了,唯一比较纠结的事情就是看书的时候没有特别留心绝对路径和相对路径的区别,在jsp中“/“表示相对于根目录的绝对路径,没有”/“表示相对于根目录的相对路径,注意以下就好啦~~~