设计一个输入页面input.jsp(使用表单)输入用户姓名,提交到OutPutServlet),一个Servlet程序(OutPutServlet,获取表单里的用户姓名,转发到Welcome.jsp),一个输出显示页面Welcome.jsp(获取Servlet程序转发过来的用户姓名,在页面中显示)
时间: 2023-12-24 21:04:26 浏览: 66
一个jsp页面中多个提交按钮提交不同的页面.zip
以下是input.jsp页面的代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>输入页面</title>
</head>
<body>
<form action="OutPutServlet" method="post">
<label for="name">请输入用户名:</label>
<input type="text" id="name" name="username">
<input type="submit" value="提交">
</form>
</body>
</html>
```
接下来,编写OutPutServlet.java的代码:
```java
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;
@WebServlet("/OutPutServlet")
public class OutPutServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public OutPutServlet() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
request.setAttribute("username", username);
request.getRequestDispatcher("Welcome.jsp").forward(request, response);
}
}
```
最后,编写Welcome.jsp页面的代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome页面</title>
</head>
<body>
<h1>欢迎您,<%= request.getAttribute("username") %>!</h1>
</body>
</html>
```
以上就是整个流程的代码实现,可以直接运行在Tomcat服务器上。
阅读全文