Understanding how to start a servlet in Eclipse

I am writing a simple reservation request for one of my homework assignments. My reservation booking website has a login, check-in, flight search, results display and JSP confirmation pages. I want to call servlets when a user tries to perform an action, for example:

The user goes to login.jsp and clicks the "Login" button, the form has a target / kernel / Login (the kernel is my main servlets, and Login is the corresponding servlet that should handle this request).

In our previous homework, we wrote our own web server, so I didn’t use Tomcat to deploy serlvet ... now that we no longer use our own web server, I am having problems figuring out how to run servlets in Eclipse .

I created a dynamic web project in Eclipse and automatically created the following folder structure:

../P3 ../P3/src ../P3/src/core ../P3/build ../P3/WebContent ../P3/WebContent/META-INF ../P3/WebContent/WEB-INF 

My login.jsp has a form that, when filled and clicked, sends the results to the Login servlet in my main package:

  <form name="input" action="src/core/Login" method=GET> Username: <input type="text" name="userName" /> <br> Password: <input type="password" name="password" /> <br> <input type="submit" value="Login" /> </form> 

Eclipse has a built-in browser, so when I run login.jsp, it loads it into the Eclipse browser and displays it. When I click the Login button, it should send the username and password to the Login servlet, but instead I get the following message:

 HTTP Status 404 - /P3/src/core/Login -------------------------------------------------------------------------------- type Status report message /P3/src/core/Login description The requested resource (/P3/src/core/Login) is not available. 

For some reason, the server does not know about the servlet ... I read a couple ( first and second ) articles on the Internet explaining possible problems, but I still get the error. I tried the solution in the first article and it did not work; the second article did not actually apply, since the server settings.xml file already contained the correct flags and directories.

Here is my WEB-INF / web.xml file:

 <?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>P3</display-name> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <servlet> <description></description> <display-name>Registration</display-name> <servlet-name>Registration</servlet-name> <servlet-class>core.Registration</servlet-class> </servlet> <servlet-mapping> <servlet-name>Registration</servlet-name> <url-pattern>/Registration</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>Register</display-name> <servlet-name>Register</servlet-name> <servlet-class>core.Register</servlet-class> </servlet> <servlet-mapping> <servlet-name>Register</servlet-name> <url-pattern>/Register</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>Login</display-name> <servlet-name>Login</servlet-name> <servlet-class>core.Login</servlet-class> </servlet> <servlet-mapping> <servlet-name>Login</servlet-name> <url-pattern>/Login</url-pattern> </servlet-mapping> </web-app> 

How do I configure my project to deploy correctly in Eclipse?

+4
source share
2 answers

The action of the form action="src/core/Login" does not match the display of the /Login servlet. The action of the form should point to the URL, and not to the path to the local file system on disk.

+4
source

How this does the homework, I’ll just show you what I think is right.

I suggest you look at the deployment descriptor . (this is the file / WEB -INF / web.xml). In this file, you define the servlets and URL mappings that clients will use to access these servlets.

If these items are not present, Tomcat does not know how to handle requests for your servlets.

If you tell NetBeans that you are creating a new servlet, unlike the new Java class, it will prompt you to get the appropriate information to configure everything.

+2
source

Source: https://habr.com/ru/post/1302681/


All Articles