传计一个输入面面inputisp使用表单输入用户姓名,提交到OutPutServet一个Servet序(OutPutServlet获取去 单里的用户姓名,转发到Welcomejsp),一个输出显示页面Welcome.jsp(获取Servlet程序转发过来的用户姓名,在页 面中显示)。
时间: 2024-01-21 21:17:09 浏览: 74
好的,这里提供一个简单的实现方式。
1. 编写表单页面 input.jsp:
```html
<!DOCTYPE html>
<html>
<head>
<title>Input Name</title>
</head>
<body>
<form action="OutputServlet" method="post">
<label for="name">Name:</label>
<input type="text" name="name" id="name" required>
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
```
2. 编写 Servlet 程序 OutputServlet.java:
```java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class OutputServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
request.setAttribute("name", name);
request.getRequestDispatcher("Welcome.jsp").forward(request, response);
}
}
```
3. 编写输出页面 Welcome.jsp:
```html
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Welcome, <%=request.getAttribute("name") %>!</h1>
</body>
</html>
```
4. 将 input.jsp 和 Welcome.jsp 放在 WebContent 目录下,将 OutputServlet.java 放在 src 目录下,并在 web.xml 中配置 Servlet:
```xml
<servlet>
<servlet-name>OutputServlet</servlet-name>
<servlet-class>OutputServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>OutputServlet</servlet-name>
<url-pattern>/OutputServlet</url-pattern>
</servlet-mapping>
```
完成后,启动 Tomcat 服务器,在浏览器中访问 input.jsp,输入姓名后提交,即可在 Welcome.jsp 页面中看到欢迎信息。
阅读全文