1、Jsp代码:
<html>
<body>
<h2>你好,请输入用户名和密码!</h2>
<%--${pageContext.request.contextPath} 代表当前项目--%>
<form action="${pageContext.request.contextPath}/login" method="get" pageEncoding:<%@ page pageEncoding="UTF-8"%>>
用户名:<input type="text" name="username">
<br>
密码:<input type="password" name="password">
<br>
<input type="submit">
</form>
</body>
</html>
2、Java代码
public class RequestTest extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
// 处理请求
String username = req.getParameter("username");
String password = req.getParameter("password");
resp.getWriter().print("你的用户名为:" + username + ";" +"<br>" + "你输入的密码为:" + password + "。");
// 重定向
// resp.sendRedirect("");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
3、web.xml映射文件
<servlet>
<servlet-name>requestTest</servlet-name>
<servlet-class>com.wjj.servlet.RequestTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>requestTest</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>