我已經創建了一個使用Eclipse第一次使用servlet和jsp的Dynamic Web Project
。servlet容器如何處理這個http請求?
下面是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中與URI的index.jsp
怎麼樣了表示代碼: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
是 「動態Web項目」 的名稱在Eclipse
我明白你的第一個答案。當你說:'你發送請求到',我沒有明確發送,而是我說'運行在服務器上'那麼,爲什麼servlet容器在第二種情況下選擇servlet'http:// localhost:8081/ServletsJSPExample/servletexample'但不是'index.jsp', – overexchange
@overxchange我不知道Eclipse如何選擇它的URL。這並不重要。如果您更改url並將您的請求發送到'http:// localhost:8081/ServletsJSPExample /',則JSP將直接提供,就像您的第一種情況一樣。您的servlet根本不會涉及。 –
您可以添加更多關於這個'this.getServletContext()'的信息嗎?我們爲什麼用它? – overexchange