问答详情
源自:1-11 Servlet获取表单数据

404问题。打不开页面!(内附所有代码&web.xml文件内容)

问题见题。

web.xml如下

<web-app version="3.0"
 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">
  <display-name></display-name>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>RegistServlet</servlet-name>
    <servlet-class>servlet.RegistServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>RegistServlet</servlet-name>
    <url-pattern>/servlet/RegistServlet</url-pattern>
  </servlet-mapping> 
  <welcome-file-list>
    <welcome-file>regist.jsp</welcome-file>
  </welcome-file-list>
</web-app>

···································································································

regist.jsp如下

<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
    <style type="text/css">
  .label{
    width: 20% 
  }
  .controler{
    width: 80% 
  }
   </style>
  
   <script type="text/javascript" src="js/Calendar.js"></script>
  </head>
 
  <body>
  <h1>用户d注册</h1>
  <hr>
  <form name="registForm" action="servlet/RegistServlet">
   <table border="0" width="800" cellspacing="0" cellpadding="0">
   <tr>
    <td class="label">用户名:</td>
    <td class="controler"><input type="text" name="username" /></td>
   </tr>
   <tr>
    <td class="label">密码:</td>
    <td class="controler"><input type="password" name="userpass" /></td>
 </tr>
   <tr>
    <td class="label">确认密码:</td>
    <td class="controler"><input type="password" name="confirmpass" /></td>
 </tr>
   <tr>
    <td class="label">电子邮件:</td>
    <td class="controler"><input type="text" name="email" /></td>
 </tr>
   <tr>
    <td class="label">性别:</td>
    <td class="controler">
     <input type="radio" name="gender" value="Male" checked="checked" />男 <%-- checked="checked"表明默认选中 --%>
     <input type="radio" name="gender" value="Female" />女
    </td>
   </tr>
   <tr>
    <td class="label">出生日期:</td>
     <td class="controler">
   <input type="text" name="birthday"
      id="control_date" size="10"
      maxlength="10" onclick="new Calendar().show(this);"
   readonly="readonly" />
     </td>
   </tr>
   <tr>
    <td class="label">爱好:</td>
  <td class="controler">
   <input type="checkbox" name="favorite" value="nba" />NBA &nbsp;
   <input type="checkbox" name="favorite" value="music" />音乐 &nbsp;
   <input type="checkbox" name="favorite" value="movie" />电影 &nbsp;
   <input type="checkbox" name="favorite" value="internet" />上网 &nbsp;
  </td>
   </tr>
   <tr>
    <td>自我介绍:</td>
    <td class="controler">
   <textarea name="introduce" rows="10" cols="40"></textarea>
  </td>
   </tr>
   <tr>
  <td class="label">接受协议:</td>
  <td class="controler">
   <input type="checkbox" name="isAccept" value="true">是否接受霸王条款
  </td>
 </tr>
 <tr>
  <td colspan="2" align="center">
   <input type="submit" value="注册"/>&nbsp;&nbsp;
   <input type="reset" value="取消"/>&nbsp;&nbsp;
  </td>
 </tr>
   </table>
  </form> 
  </body>
</html>

············································································································

username.jsp如下

<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'userinfo.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
   <style type="text/css">
  .title{
   width: 30%; 
   background-color: #CCC;
   font-weight: bold;
  }
  .content{
      width:70%;
      background-color: #CBCFE5;
  }
 
   </style> 
  </head>
 
  <body>
 <body>
    <h1>用户信息</h1>
    <hr>
    <center> 
    <jsp:useBean id="regUser" class="com.entity.User" scope="session" />;
     <table width="600" cellpadding="0" cellspacing="0" border="1">
        <tr>
          <td class="title">用户名:</td>  <%-- name为实例化对象的名字,property为要取得的属性 --%>
          <td class="content">&nbsp;<jsp:getProperty name="regUser" property="username"/></td>
        </tr>
        <tr>
          <td class="title">密码:</td>
          <td class="content">&nbsp;<jsp:getProperty name="regUser" property="password"></td>
        </tr>
        <tr>
          <td class="title">性别:</td>
          <td class="content">&nbsp;<jsp:getProperty name="regUser" property="gender"/></td>
        </tr>
        <tr>
          <td class="title">E-mail:</td>
          <td class="content">&nbsp;<jsp:getProperty name="regUser" property="email"/></td>
        </tr>
        <tr>
          <td class="title">出生日期:</td>
          <td class="content">&nbsp;
         <%
          SimpleDateFormat sdf = new SimpleDateFormat("YYYY年MM月DD日");
          String date = sdf.format(regUser.getBirthday());
         %>
         <%=date %>
          </td>
        </tr>
        <tr>
          <td class="title">爱好:</td>
          <td class="content">&nbsp;
          <%
           String[] favorite = regUser.getFavorites();
           for (String f:favorite){
          %>
            <%=f %>&nbsp;&nbsp;
          <%
           }
          %>
          </td>
        </tr>
        <tr>
          <td class="title">自我介绍:</td>
          <td class="content">&nbsp;<jsp:getProperty name="regUser" property="introduce"/></td>
        </tr>
        <tr>
          <td class="title">是否遵守协议:</td>
          <td class="content">&nbsp;<jsp:getProperty name="regUser" property="flag"/></td>
        </tr>
     </table>
    </center>
  </body>
</html>


·····························································································

RegistServlet如下

public class RegistServlet extends HttpServlet {


 public RegistServlet() {
  super();
 }

 /**
  * Destruction of the servlet. <br>
  */
 public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
 }

 /**
  * The doGet method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to get.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  doPost(request,response);
 }

 /**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to post.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  User user = new User();
  String username,password,gender,email,introduce,isAccept;
  Date birthday;
  String[] favorite;
  SimpleDateFormat sdf = new SimpleDateFormat("YYYY年MM月DD日hh:mm:ss");
  
  try{
   username = request.getParameter("username");
   password = request.getParameter("password");
   gender = request.getParameter("gender");
   email = request.getParameter("email");
   introduce = request.getParameter("introduce");
   birthday = sdf.parse(request.getParameter("birthday"));
   favorite = request.getParameterValues("favorite");
   isAccept = request.getParameter("isAccept");
   
   user.setAccept(isAccept.equals("true")?true:false);
   user.setBirthday(birthday);
   user.setEmail(email);
   user.setFavorites(favorite);
   user.setGender(gender);
   user.setIntroduce(introduce);
   user.setPassword(password);
   user.setUsername(username);
   
   //将成功注册用户保存到session
   request.getSession().setAttribute("regUser", user);
   //跳转(转发)到成功页面
   request.getRequestDispatcher("../userinfo.jsp").forward(request, response);
  }catch(Exception e){
   e.printStackTrace();
  }
 }

 /**
  * Initialization of the servlet. <br>
  *
  * @throws ServletException if an error occurs
  */
 public void init() throws ServletException {
  // Put your code here
 }

}

·······························································································

输入路径绝对没错(不要告诉我这个了···)求解!

提问者:陷入编程无法自拔的江北 2016-11-02 19:29

个回答

  • WTANG
    2016-11-09 23:21:06
    已采纳

    你应该直接把老师的jsp文件拷进来了,表单<form name="regForm" action="RegServlet" method="post" >

    action要与你的配置文件中<url-pattern>/RegServlet</url-pattern>要相同,老师给的action是action="servlet/RegServlet",你需要改成自己的。


  • 慕粉4005235
    2016-12-06 22:05:49

    求问怎么解决的啊,我也遇到404问题,谢谢