Understanding how to run a servlet in Eclipse
I'm writing a simple flight reservation application for one of my homeworks. My flight reservation web site has a login, registration, flight search, result display and confirmation JSP pages. I want to invoke servlets when the user attempts to perform an action, for example:
The user goes to login.jsp and clicks the Login button, the form has a target /core/Login (core is my core servlets and Login is the corresponding servlet that should handle that request).
In a previous homework we wrote our own web server, so I didn't use Tomcat to deploy the serlvet... now that we're no longer using our own web server, I'm having some trouble figuring out how to get the servlets to run in Eclipse.
I created a Dynamic Web Project in Eclipse and it automatically generated 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 which when filled out and clicked submits the results to the Login servlet in my core 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 the login.jsp it loads it in the Eclipse browser and displays it. When I click the Login button it's supposed to send the userName and the password to the Login servlet, but I get the following error instead:
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 doesn't know about the servlet... I've read a couple (first and second) of articles online explaining the possible problems, but I'm still getting the error. I tried the solution in the first article and it didn't work; the second article didn't really 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</开发者_StackOverflow中文版url-pattern>
</servlet-mapping>
</web-app>
How should I setup my project so it properly deploys in Eclipse?
The form action action="src/core/Login"
doesn't match the servlet mapping /Login
. The form action should point to an URL, not to a local disk file system path.
As it's homework, I'll just point you in what I think is the right direction.
I suggest you look at your deployment descriptor. (it's the /WEB-INF/web.xml file). In that file, you define the servlets and the URL mappings that clients will use to access those servlets.
Unless these elements are present, Tomcat does not know how to serve requests for your servlets.
If you tell NetBeans you are creating a new Servlet, as opposed to a new Java Class, it will prompt you for the appropriate information to set everything up.
精彩评论