我第一次使用 Eclipse 创建了一个动态 Web 项目
来使用 servlet 和 jsp。
下面是servlet代码,
package com.example.tutorial;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletExample extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String firstName = request.getParameter("firstname");
String lastName = request.getParameter("lastname");
out.println(firstName + " " + lastName);
}
}
以及相应的web.xml,
<?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>ServletsJSPExample</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>ServletExample</display-name>
<servlet-name>ServletExample</servlet-name>
<servlet-class>com.example.tutorial.ServletExample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletExample</servlet-name>
<url-pattern>/servletexample</url-pattern>
</servlet-mapping>
</web-app>
我还编写了 index.jsp
,其形式如下:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
<form action="servletexample" method="post" >
<table border="0">
<tr>
<td>First Name:</td> <td><input type="text" name="firstname" /></td>
</tr>
<tr>
<td>Last Name:</td> <td><input type="text" name="lastname" /></td>
</tr>
<tr>
<td colspan="2"> <input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
在我的第一个案例中,在 Eclipse 中,如果我选择 Run As -> Run On Server
,Eclipse 中的浏览器将显示 index.jsp
的表示代码,其 URI:http://localhost:8081/ServletsJSPExample/
我稍后在 servlet 中添加以下代码行,
this.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
如下图,
public class ServletExample extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
this.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
String firstName = request.getParameter("firstname");
String lastName = request.getParameter("lastname");
out.println(firstName + " " + lastName);
}
}
在第二种情况下,在 Eclipse 中,如果我选择 Run As -> Run On Server
,Eclipse 中的浏览器将显示 index.jsp
的表示代码,其 URI:http://localhost:8081/ServletsJSPExample/servletexample
所以,
对于这两种情况,这个 URI 是如何改变的?
在添加这行代码之前和之后,控制如何从 servlet 容器流向我的应用程序 ServletsJSPExample
this.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response) );
请帮我理解这一点!!!
注意:ServletsJSPExample
是 eclipse 中的“动态 Web 项目”名称
最佳答案
首先,忘记 Eclipse 以及如何在其中启动程序。这是关于 Servlet、它们的 url 映射以及您点击的 url。
在第一个示例中,您点击了
http://localhost:8081/ServletsJSPExample/
假设 ServletsJSPExample
是您的上下文根,您将到达应用程序的根。由于您注册了一个 welcome-file
(这是一个 JSP)
<welcome-file>index.jsp</welcome-file>
Servlet 容器将直接为其提供服务。
在第二种情况下,您将请求发送至
http://localhost:8081/ServletsJSPExample/servletexample
其路径为/servletexample
。您已将 ServletExample
servlet 映射到
<url-pattern>/servletexample</url-pattern>
因此 Servlet 容器选择此 ServletExample
servlet 来处理请求。处理请求意味着调用其 service
方法,该方法继续调用
this.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
调用 getRequestDispatcher
容器
Returns a
RequestDispatcher
object that acts as a wrapper for the resource located at the given path.
在本例中,位于给定路径的资源是 JSP(另一个 Servlet
)。通过调用 forward
,您实际上正在调用 JSP servlet 上的 service
。这将呈现 JSP 的内容并将其作为响应正文发送。
关于java - servlet容器如何处理这个http请求呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30560110/