简体   繁体   中英

Jsp connecting to servlet HTTP Status 404

I am trying to make a login servlet. I have a login.jsp from where i take data using a form, a LoginServlet where i use HttpSession and finally my web.xml I searched here in stackoverflow and i corrected some mistakes i found. Even though my LoginServlet can't be found:

HTTP Status 404 - /test_pages/main_pages/com.register/LoginServlet

type: Status report

message: /test_pages/main_pages/com.register/LoginServlet

description: The requested resource is not available.

I saw in Status 404 that the path to my servlet is this: /test_pages/main_pages/com.register/LoginServlet

This is wrong path. My servlet is not in main_pages. I am writing in ecipse. I have to make a project in order to run all thes in apache tomcat and it confuses me a lot. My LoginServlet is in Java Resources/src/com.register. "com.register" is my package.

Can you tell me a proper way to write the action in my form? Do you think the problem is somewhere else? Please help me! Thanks in advance!

This is my code:

login.jsp

 .
 .
 .
 <form action="com.register/LoginServlet" method="post">
 <p>
 <label id="upodeiksh">username</label>
 <br />
 <input type="text" name="username" id="koutaki" required/>
 </p>
 <br />
 <p>
 <label id="upodeiksh">password</label>
 <br />
 <input type="password" name="password" id="koutaki" required/>
 </p>
 <br />
 <input type="submit" name="upload" value="login" id="submit_button"/>
 </form>

LoginServlet.java

package com.register;

import java.io.IOException;  
import java.io.PrintWriter;  

import java.sql.*;

import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.http.HttpSession;

public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

         response.setContentType("text/jsp");  
         PrintWriter out=response.getWriter();
         request.getRequestDispatcher("test.jsp").include(request, response);

         String name=request.getParameter("username");  
         String password=request.getParameter("password");
         String psw = null;

         Connection con = null;
         PreparedStatement pr = null;
         ResultSet rs = null;

         try
         {
             Class.forName("com.mysql.jdbc.Driver");
             con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ted","root","1234");

             pr = con.prepareStatement("select * from ted.users where usrname=?;");

             pr.setString(1, name);

             rs = pr.executeQuery();

             //An den brethei to onoma
             if(rs == null)
             {
                out.print("Το όνομα χρήστη : "+name+" δεν υπάρχει");
                request.getRequestDispatcher("main_pages/login.jsp").include(request, response);
             }

             else
             {
                 //An brethei to onoma
                 while(rs.next())
                 {
                     psw = rs.getString("psw");
                 }

                 if(psw != password)
                 {
                     out.print("Λάθος κωδικός πρόσβασης");
                    request.getRequestDispatcher("main_pages/login.jsp").include(request, response);
                 }

                 else if(psw == password)
                 {
                    out.print("Γεια");
                    HttpSession session=request.getSession();  
                    session.setAttribute("name",name);  
                 }
             }

         }

         catch (Exception e)
         {
             System.out.println("The error is=="+e.getMessage()); 
         }

         finally
         {
             try
             {
                 con.close();
             } 

             catch (SQLException e)
             {
                 e.printStackTrace();
             }
         }
    }
}

web.xml

    .
    .
      <servlet>  
        <description></description>  
        <display-name>LoginServlet</display-name>  
        <servlet-name>LoginServlet</servlet-name>  
        <servlet-class>com.register.LoginServlet</servlet-class>  
      </servlet>  
      <servlet-mapping>  
        <servlet-name>LoginServlet</servlet-name>  
        <url-pattern>/LoginServlet</url-pattern>  
      </servlet-mapping>  
      <servlet> 
    .
    .

Your servlet mapping is wrong. Change your form action like this
<form action="LoginServlet" method="post">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM