Reputation: 177
My goal: to access a .htm file, and pass the user input to the invoked servlet and display the content.
What i did: I used eclipse Juno to create a dynamic project:ServeletTest. The structure of the project is as followed:
The servlet file is MyServlet.java and the related code is:
package ylai.Servlet.test;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
/**
* Servlet implementation class MyServlet
*/
@WebServlet(description = "test servlet", urlPatterns = { "/MyServlet" })
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MyServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String info = request.getParameter("info") ;
PrintWriter out = response.getWriter() ;
out.println("<html>") ;
out.println("<head><title>Hello Servlet</title></head>") ;
out.println("<body>") ;
out.println("<h1>" + info + "</h1>") ;
out.println("</body>") ;
out.println("</html>") ;
out.close() ;
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doGet(request, response);
}
}
The html file is input.htm . And the detail code is:
<html>
<head><title>This is html file</title></head>
<body>
<form action="myservlet" method="post">
Type something:<input type="text" name="info">
<input type="submit" value="submit">
</form>
</body>
</html>
And the web.xml is defined as :
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>ylai.Servlet.test.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/myservlet</url-pattern>
</servlet-mapping>
</web-app>
When i run the input.htm using Built-in Tomcat within the Eclipse, it works fine, and the input content in the input.htm can be displayed by MyServlet.java. The screen shot is as followed:
It seems works fine.
My Question:
If i want to modify the value of in the web.xml as
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/myservletURL</url-pattern>
</servlet-mapping>
What i expected was once the input.htm is submitted, it will invoked the serlvet and the web page address should be:
http://localhost:8080/ServeletTest/myservletURL
But the display page address is still, does not change :
http://localhost:8080/ServeletTest/myservlet
with HTTP Status 404 error.
It looks weird!!! The mechanism should be: When i submit the input.htm page, it will invoke servlet by servlet-name in the web.xml. In this case, servlet-name is myservlet. Tomcat will use servlet-name to find the actual location of servlet file: MyServlet.java and execute it. The redirect page address would be depends on what you define in . In this case, it should /ServeletTest/myservletURL But right now. Servlet file can not be invoked and the page address is not what i expect.
Do i have wrong understand on the servlet invoke mechanism or others?
Upvotes: 0
Views: 23358
Reputation: 10943
LifeCycleServlet--@WebServlet("/LifeCycleServlet")
MyServlet--@WebServlet(description = "test servlet", urlPatterns = { "/MyServlet" })
delete these lines because here you have mentioned url as MyServlet
or
Change this urlpattern { "/MyServlet" } also
Upvotes: 1
Reputation: 626
If you changed the url-pattern to myservletURL, you will also need to update the form action to target this new url.
Upvotes: 2