在user.jsp中输入用户名,选择性别,选择学历(博士,硕士,本科,大专,中学),填写email,点击提交,提交到下一个页面login.jsp

 

 

login.jsp输出用户填写的信息,注意中文乱码问题,如出现用

 

 

user.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户信息</title>
</head>
<% String path = request.getContextPath();
    
%>
<body>
    <form  action="<%=request.getContextPath() %>/threeServlet" method="post">
    <table border="1px solid">
    <tr>
    <td>用户名:</td><td><input type="text" name="username"></td>
    </tr>
    <tr><td>性别:</td><td><input type="radio" name="sex" value="男">男<input type="radio"name="sex" value="女">女</td></tr>
    <tr><td>学历:</td><td><select name="edu">
    <option value="硕士">硕士</option>
    <option value="本科">本科</option>
    <option value="大专">大专</option>
    </select></td></tr>
    <tr><td>E-mail:</td><td><input type="email" name="email"></td></tr>
    <tr ><td colspan="2" style="text-align:center"><button>重填</button><button type="submit">提交</button></td></tr>
    </table>
    </form>
    <!--  <%String name1=request.getParameter("username"); %>
    <%String sex1 = request.getParameter("sex"); %>
    <%String edu1 = request.getParameter("edu"); %>
    <%String email1 = request.getParameter("email"); %>-->
    
</body>
<script>
    
</script>
</html>

servlet内容转接

package net.wanho.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ThreeServlet
 */
public class ThreeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //    request.setAttribute("username","name1");
        request.setAttribute("wechart", "13823223212");
        request.getRequestDispatcher("login.jsp").forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<% request.setCharacterEncoding("utf-8");%>
    <table border="1px solid" >
    
    <tr>
    <td>用户名:</td><td><%=request.getParameter("username")%></td>
    </tr>
    <tr><td>性别:</td><td><%=request.getParameter("sex") %></td></tr>
    <tr><td>学历:</td><td><%=request.getParameter("edu") %></td></tr>
    <tr><td>E-mail:</td><td><%=request.getParameter("email") %></td></tr>
    <tr>
<td>微信:</td>
<td><%=request.getAttribute("wechart")%></td>
</tr>
    </table>
</body>
</html>

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" id="WebApp_ID" version="2.5">
  <display-name>HomeWork3_3</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>ThreeServlet</display-name>
    <servlet-name>ThreeServlet</servlet-name>
    <servlet-class>net.wanho.servlet.ThreeServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ThreeServlet</servlet-name>
    <url-pattern>/threeServlet</url-pattern>
  </servlet-mapping>
</web-app>

 

这一题里面主要要注意的是标签<form action="<%=request.getContextPath()%>/threeServlet " method="post">

这里面的request.getContextPath()%>/threeServlet指的是绝对路径,/threeServlet是servlet的映射url路径,要注意。

posted @ 2020-02-15 23:51  技术白菜  阅读(844)  评论(0编辑  收藏  举报