Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么;回复:sendRedirect“;不';不行?_Java_Html_Derby_Javadb - Fatal编程技术网

Java 为什么;回复:sendRedirect“;不';不行?

Java 为什么;回复:sendRedirect“;不';不行?,java,html,derby,javadb,Java,Html,Derby,Javadb,这是我的登录部分,我需要将用户输入与数据库中的数据进行比较 因此,如果讲师试图进入系统,他或她将进入讲师界面(讲师.html),学生也会进入该界面 但现在,当我尝试使用讲师或学生ID进入系统时,系统将引导我进入登录界面 我希望有人能帮我解决这个问题:) 这是对JavaDB(LogIn.Java)的查询 这是其余的代码(LogIn.java) protectedvoidprocessrequest(HttpServletRequest请求,HttpServletResponse响应)抛出Servl

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

因此,如果讲师试图进入系统,他或她将进入讲师界面(讲师.html),学生也会进入该界面

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

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

这是对JavaDB(LogIn.Java)的查询

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

protectedvoidprocessrequest(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
字符串matricI=request.getParameter(“matricin”);
字符串passwordI=request.getParameter(“passwordin”);
setContentType(“text/html;charset=UTF-8”);
PrintWriter out=response.getWriter();
试一试{
out.println(“\n”+
“\n”+
“SPEDT|UKM\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“切换导航\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
"");
//加载数据库驱动程序
试一试{
forName(“org.apache.derby.jdbc.ClientDriver”);
}捕获(ClassNotFoundException cnfe){
系统输出打印LN(cnfe);
}
//创建到contacts db数据源的连接
试一试{
connection=DriverManager.getConnection(dbURI,“demo”,“demo”);
}捕获(SQLException sqle){
System.out.println(sqle);
}
//准备将数据插入表的语句
试一试{
准备好的报表=连接。准备好的报表(str);
setAttribute(“matricin”,matricI);
setAttribute(“passwordin”,passwordI);
语句st=connection.createStatement();
结果集rs=st.executeQuery(str);
字符串m=矩阵;
字符串p=密码i;
while(rs.next()){
字符串矩阵=rs.getString(“学生ID”);
字符串密码=rs.getString(“STUD_密码”);
String matricL=rs.getString(“讲师ID”);
字符串密码l=rs.getString(“选择密码”);
如果(m.equals(矩阵)和p.equals(密码)){
response.sendRedirect(“http://localhost:8080/Spedt/StudentInput");
返回;}
else if(m.equals(matricL)和p.equals(passwordL)){
response.sendRedirect(“http://localhost:8080/Spedt/Lecturer.html");
返回;}
否则{
out.println(“

Sila masukkan No.Matrik and Kata Laluan yang betul!

”); } } } 捕获(SQLException sqle){ System.out.println(sqle); } Connection.close();//关闭数据库连接 }捕获(例外e){ out.println(e.getMessage());//打印错误。 }最后{ out.close(); } } 受保护的void doGet(HttpServletRequest请求、HttpServletResponse响应) 抛出ServletException、IOException{ processRequest(请求、响应); } 受保护的void doPost(HttpServletRequest请求、HttpServletResponse响应) 抛出ServletException、IOException{ processRequest(请求、响应); } 公共字符串getServletInfo(){ 返回“简短描述”; }//
}

这是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


输入数据
mypkg.InputData
获取数据
mypkg.GetData
登录
mypkg.LogIn
讲师
我的导师
学生
我的学生
注销
mypkg.LogOut
讲师
mypkg.PUT
GetData讲师
mypkg.getData讲师
新闻服务
mypkg.NewServlet
输入讲师
mypkg.input讲师
GetDataSelect
mypkg.GetDataLect
学生投入
mypkg.StudentInput
信息输入
mypkg.InputStudent
GetDataStud
mypkg.GetDataStud
输入数据
/输入数据
获取数据
/获取数据
登录
/登录
讲师
/讲师
学生
/学生
注销
/注销
讲师
/讲师
GetData讲师
/GetData讲师
新闻服务
/新闻服务
输入讲师
/输入讲师
GetDataSelect
/GetDataSelect
学生投入
/学生投入
信息输入
/输入学生
GetDataStud
/GetDataStud
30

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

在这种情况下,控件没有到达
servlet
,那么您希望如何运行
重定向

您编写的html的动作部分
action=”http://localhost:8080/Spedt/LogIn“
您应该对您在
web.xml
文件中定义的servlet使用操作

看到你的xm了吗
 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>
<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>
<servlet>
        <servlet-name>LogIn</servlet-name>
        <servlet-class>mypkg.LogIn</servlet-class>
</servlet>
<form class="navbar-form navbar-right" role="form" method="get" action="LogIn">
<button type="submit" class="btn btn-success" onclick="action">Masuk</button>