设计一个输入页面input.jsp(使用表单输入用户姓名,提交到OutPutServlet),一个servlet程序(OutPutServlet,获取表单里的用户姓名,转发到Welcome.jsp),一个输出显示页面Welcome.jsp(获取Servlet程序转发过来的用户姓名,在页面中显示)
时间: 2024-01-21 16:17:32 浏览: 77
jsp+servlet实现用户登陆
5星 · 资源好评率100%
input.jsp:
```html
<!DOCTYPE html>
<html>
<head>
<title>Input Page</title>
</head>
<body>
<form action="OutPutServlet" method="post">
<label for="name">Enter your name:</label>
<input type="text" id="name" name="name"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
```
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);
}
}
```
Welcome.jsp:
```html
<!DOCTYPE html>
<html>
<head>
<title>Welcome Page</title>
</head>
<body>
<h2>Welcome <%=request.getAttribute("name")%>!</h2>
</body>
</html>
```
在web.xml中配置servlet和servlet-mapping:
```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>
```
以上代码实现了一个简单的输入页面,输入用户姓名后提交到OutPutServlet,OutPutServlet将用户姓名存储在request中并转发到Welcome.jsp,在Welcome.jsp中显示欢迎信息。
阅读全文