0

这是我的 servlet,但我无法导航到我想要访问的下一个 jsp。我得到的错误是:请求的资源不可用/用户控制器。

这是我的 servlet - 用户控制器

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.SwingUtilities;

public class UserController extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static String INSERT_OR_EDIT = "/user.jsp";
    private static String LIST_USER = "/listUser.jsp";
    public static String PARAM_USERNAME = "uname";
    public static String PARAM_PASSWORD = "pass";
    private UserDao dao;

    public UserController() {
        super();
        dao = new UserDao();
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        String forward="";
String act = request.getParameter("act");
         if (act != null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("login")) {
            forward= "/Login.jsp";
            }

         else if (act != null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("delete"))
        {
            int userId = Integer.parseInt(request.getParameter("userId"));
            dao.deleteUser(userId);
            forward = LIST_USER;
            request.setAttribute("users", dao.getAllUsers());    
        } else if (act!=null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("edit")){
            forward = INSERT_OR_EDIT;
            int userId = Integer.parseInt(request.getParameter("userId"));
            User user1 = dao.getUserById(userId);
            request.setAttribute("user", user1);
        } else if (act!=null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("listUser")){
            forward = LIST_USER;
            request.setAttribute("users", dao.getAllUsers());
        } else if (act!=null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("register")){
            forward = LIST_USER;
            request.setAttribute("users", dao.getAllUsers());
        } else 
            forward = "/Login.jsp";

        RequestDispatcher view = request.getRequestDispatcher(forward);
        view.forward(request, response);
        } 



    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = new User();
        Details details = new Details();
        user.setFirstName(request.getParameter("firstName"));
        user.setLastName(request.getParameter("lastName"));
        details.setUsername(request.getParameter("uname"));
        details.setPassword(request.getParameter("pass"));
      //  details.setId(request.getParameter("id"));
        String str=request.getParameter("username");
        String str1=request.getParameter("password");



if(str.equalsIgnoreCase("shreya")&&str1.equalsIgnoreCase("singh"))
         System.out.println("Login!");
     else
     {
      System.out.println("Login failed!");


      request.setAttribute("users",dao.getAllUsers());

            request.getRequestDispatcher("/listUser.jsp").forward(request, response); 

        }
     }
}

这是我的 listUser 页面:

<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Show All Users</title>
</head>
<body>
    <table border=1>
        <thead>
            <tr>
                <th>User Id</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>DOB</th>
                <th>Email</th>

            </tr>
        </thead>
        <tbody>
            <c:forEach items="${users}" var="user">
                <tr>
                    <td><c:out value="${user.userid}" /></td>
                    <td><c:out value="${user.firstName}" /></td>
                    <td><c:out value="${user.lastName}" /></td>
                    <td><fmt:formatDate pattern="yyyy-MMM-dd" value="${user.dob}" /></td>
                    <td><c:out value="${user.email}" /></td>


                </tr>
            </c:forEach>
        </tbody>
    </table>
    <p><a href="/UserController?act=register">Add User</a></p>
    <p><a href="logout.jsp">Logout</a></p>
</body>
</html>

这是我接下来要访问的 jsp 页面:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Registration</title>
    </head>
    <body>
        <form method="post" action="/UserController?act=save">
            <center>
            <table border="1" width="30%" cellpadding="5">
                <thead>
                    <tr>
                        <th colspan="2">Enter Information Here</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>First Name</td>
                        <td><input type="text" name="firstName" value="" /></td>
                    </tr>
                    <tr>
                        <td>Last Name</td>
                        <td><input type="text" name="lastName" value="" /></td>
                    </tr>
                    <tr>
                        <td>User Name</td>
                        <td><input type="text" name="uname" value="" /></td>
                    </tr>
                    <tr>
                        <td>Password</td>
                        <td><input type="password" name="pass" value="" /></td>
                    </tr>
                    <tr>
                        <td><input type="submit" value="Submit" /></td>
                        <td><input type="reset" value="Reset" /></td>
                    </tr>
                    <tr>
                        <td colspan="2">Already registered!! <a href="index.jsp">Login Here</a></td>
                    </tr>
                </tbody>
            </table>
            </center>
        </form>
    </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>SimpleJspServletDB2</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>UserController</display-name>
    <servlet-name>UserController</servlet-name>
    <servlet-class>com.shreya.controller.UserController</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>UserController</servlet-name>
    <url-pattern>/UserController</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>LoginServlet</display-name>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>com.shreya.controller.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>LoginServlet1</display-name>
    <servlet-name>LoginServlet1</servlet-name>
    <servlet-class>com.shreya.controller.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginServlet1</servlet-name>
    <url-pattern>/LoginServlet1</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>UserController1</display-name>
    <servlet-name>UserController1</servlet-name>
    <servlet-class>com.shreya.controller.UserController1</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>UserController1</servlet-name>
    <url-pattern>/UserController1</url-pattern>
  </servlet-mapping>
</web-app>
4

0 回答 0