为什么“response.sendRedirect”不起作用?

时间:2014-06-12 05:36:43

标签: java html derby javadb

这是我的登录部分,我需要将用户输入与数据库中的数据进行比较。

因此,如果讲师试图进入系统,他或她将进入讲师界面(讲授),同样也会进入学生。

但是现在,当我尝试使用讲师或学生ID进入系统时,系统会将我引导至登录界面。

我希望有人可以帮我解决这个问题:)

这是Java DB(LogIn.java)的查询

public class LogIn extends HttpServlet {
static final String dbURI = "jdbc:derby://localhost:1527/webdb";
String str = SELECT DEMO.REGISTRATION.*, \n" +
              DEMO.STUDENT.STUD_PASSWORD,DEMO.LECTURER.LECT_PASSWORD 
              FROM DEMO.REGISTRATION 
              LEFT OUTER JOIN DEMO.STUDENT  
              ON REGISTRATION.STUDENT_ID = STUDENT.STUDENT_ID
              LEFT OUTER  JOIN DEMO.LECTURER 
              ON REGISTRATION.LECTURER_ID = LECTURER.LECTURER_ID;";

Connection theConnection = null;

这是其余的代码(LogIn.java)

 protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String matricI = request.getParameter("matricin");
String passwordI = request.getParameter("passwordin");

    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    try {
        out.println("<html>\n" +
     "  <head>\n" +
     "    <title>SPEDT | UKM</title>\n" +
     "    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" +
     "    <link rel=\"stylesheet\" href=\"//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css\">\n" +
     "    <link rel=\"stylesheet\" href=\"//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css\">\n" +
     "    <script src=\"//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js\"></script>\n" +
     "  </head>\n" +
     "  \n" +
     "  <body>\n" +
     "    \n" +
     "    <div class=\"navbar navbar-inverse \" role=\"navigation\">\n" +
     "      <div class=\"container\">\n" +
     "        <div class=\"navbar-header\">\n" +
     "          <button type=\"button\" class=\"navbar-toggle\" data-toggle=\"collapse\" data-target=\".navbar-collapse\">\n" +
     "            <span class=\"sr-only\">Toggle navigation</span>\n" +
     "            <span class=\"icon-bar\"></span>\n" +
     "            <span class=\"icon-bar\"></span>\n" +
     "            <span class=\"icon-bar\"></span>\n" +
     "          </button>\n" +
     "          <a class=\"navbar-brand\" href=\"#\">Sistem Penilaian Esei Dalam Talian</a>\n" +
     "        </div>\n" +
     "        <div class=\"navbar-collapse collapse\">\n" +
     "         <form class=\"navbar-form navbar-right\" role=\"form\" method=\"get\" action=\"http://localhost:8080/Spedt/LogIn\">\n" +
     "            <a class=\"btn btn-danger\" role=\"button\" href=\"http://localhost:8080/Spedt/start.html\">Keluar</a>\n" +
     "          </form>\n" +
     "        </div><!--/.navbar-collapse -->\n" +
     "      </div>\n" +
     "    </div>\n" +
     "</html>");
        // Load database driver
        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
    } catch (ClassNotFoundException cnfe){
                System.out.println(cnfe);
    }
        // Create a Connection to contacts db Data source
        try {
            theConnection = DriverManager.getConnection(dbURI,"demo","demo");
    } catch (SQLException sqle) {
                System.out.println(sqle);
    }
        // prepare statement for inserting data into table
        try {
            PreparedStatement theStatement=theConnection.prepareStatement(str);

            request.setAttribute("matricin",matricI);
            request.setAttribute("passwordin",passwordI);

            Statement st = theConnection.createStatement();
    ResultSet rs = st.executeQuery(str);
            String m = matricI;
            String p = passwordI;

            while (rs.next()) {
                  String matricS = rs.getString("STUDENT_ID");
                  String passwordS = rs.getString("STUD_PASSWORD");
                  String matricL = rs.getString("LECTURER_ID");
                  String passwordL = rs.getString("LECT_PASSWORD");

                  if(m.equals(matricS) && p.equals(passwordS)){
                      response.sendRedirect("http://localhost:8080/Spedt/StudentInput");
                  return;}
                  else if(m.equals(matricL) && p.equals(passwordL)){
                      response.sendRedirect("http://localhost:8080/Spedt/Lecturer.html");
                  return;}
                  else {
                     out.println("<p class=\"bg-danger container\">Sila masukkan No. Matrik dan Kata Laluan yang betul !</p>");
                  }
            }
        }

        catch (SQLException sqle) {
            System.out.println(sqle);
        }

            theConnection.close(); //Close database Connection
    }catch(Exception e) {
        out.println(e.getMessage());//Print trapped error.
} finally {
        out.close();
    }
}

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    processRequest(request, response);
} 

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    processRequest(request, response);
}

public String getServletInfo() {
    return "Short description";
}// </editor-fold>

}

这是html

<form class="navbar-form navbar-right" role="form" method="get" action="http://localhost:8080/Spedt/LogIn">
        <div class="form-group">
          <input type="text" name="matricin" placeholder="No. Matrik" class="form-control">
        </div>
        <div class="form-group">
          <input type="password" name="passwordin" placeholder="Kata Laluan" class="form-control">
        </div>
        <button type="submit" class="btn btn-success">Masuk</button>
      </form>

这是我的web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> <servlet> <servlet-name>InputData</servlet-name> <servlet-class>mypkg.InputData</servlet-class> </servlet> <servlet> <servlet-name>GetData</servlet-name> <servlet-class>mypkg.GetData</servlet-class> </servlet> <servlet> <servlet-name>LogIn</servlet-name> <servlet-class>mypkg.LogIn</servlet-class> </servlet> <servlet> <servlet-name>Lecturer</servlet-name> <servlet-class>mypkg.Lecturer</servlet-class> </servlet> <servlet> <servlet-name>Student</servlet-name> <servlet-class>mypkg.Student</servlet-class> </servlet> <servlet> <servlet-name>LogOut</servlet-name> <servlet-class>mypkg.LogOut</servlet-class> </servlet> <servlet> <servlet-name>LecturerInput</servlet-name> <servlet-class>mypkg.LecturerInput</servlet-class> </servlet> <servlet> <servlet-name>GetDataLecturer</servlet-name> <servlet-class>mypkg.GetDataLecturer</servlet-class> </servlet> <servlet> <servlet-name>NewServlet</servlet-name> <servlet-class>mypkg.NewServlet</servlet-class> </servlet> <servlet> <servlet-name>InputLecturer</servlet-name> <servlet-class>mypkg.InputLecturer</servlet-class> </servlet> <servlet> <servlet-name>GetDataLect</servlet-name> <servlet-class>mypkg.GetDataLect</servlet-class> </servlet> <servlet> <servlet-name>StudentInput</servlet-name> <servlet-class>mypkg.StudentInput</servlet-class> </servlet> <servlet> <servlet-name>InputStudent</servlet-name> <servlet-class>mypkg.InputStudent</servlet-class> </servlet> <servlet> <servlet-name>GetDataStud</servlet-name> <servlet-class>mypkg.GetDataStud</servlet-class> </servlet> <servlet-mapping> <servlet-name>InputData</servlet-name> <url-pattern>/InputData</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>GetData</servlet-name> <url-pattern>/GetData</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>LogIn</servlet-name> <url-pattern>/LogIn</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Lecturer</servlet-name> <url-pattern>/Lecturer</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Student</servlet-name> <url-pattern>/Student</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>LogOut</servlet-name> <url-pattern>/LogOut</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>LecturerInput</servlet-name> <url-pattern>/LecturerInput</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>GetDataLecturer</servlet-name> <url-pattern>/GetDataLecturer</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>NewServlet</servlet-name> <url-pattern>/NewServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>InputLecturer</servlet-name> <url-pattern>/InputLecturer</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>GetDataLect</servlet-name> <url-pattern>/GetDataLect</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>StudentInput</servlet-name> <url-pattern>/StudentInput</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>InputStudent</servlet-name> <url-pattern>/InputStudent</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>GetDataStud</servlet-name> <url-pattern>/GetDataStud</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> </web-app>

我希望有人能帮我解决这个问题。谢谢:)

2 个答案:

答案 0 :(得分:1)

在您的代码中,您不需要在URL中指定完整的action,您可以在abc.jsp中指定ServletNameweb.xml }。

<form class="navbar-form navbar-right" role="form" method="get" action="LogIn">

其他您需要在表单的“提交”按钮中指定onClick

<button type="submit" class="btn btn-success" onclick="action">Masuk</button>

修改

  

我每次尝试登录时都会收到此信息。   本地主机:8080 / Spedt /登录matricin = A138&安培; passwordin = 92

所以它的工作并且您需要在LogIn Servlet中执行一些操作,因为doGet方法为空并且更好地使用{{1}对于LogIn !!!

答案 1 :(得分:0)

哦~~ ..

在这种情况下,控件未达到servlet,那么您希望如何运行redirect !!

在你的html的行动部分,你写了action="http://localhost:8080/Spedt/LogIn" 您应该对在web.xml文件

中定义的servlet使用操作

请参阅您的xml文件

<servlet>
        <servlet-name>LogIn</servlet-name>
        <servlet-class>mypkg.LogIn</servlet-class>
</servlet>

如果您写action=LogIn,那么您将转到mypkg.LogIn servlet