HttpServletRequest 接口
获取前端浏览器用户提交的数据:
String getParameter(String name)
Map<String,String[]> getParameterMap()
Enumeration<String> getParameterNames()
String[] getParameterValues(java.lang.String name)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>学生登录系统</h2>
<form action="/htmm/htmservlet" method="post">
username<input type="text" name="username"/><br>
password<input type="password" name="userpwd"/><br>
interest:
smoke<input type="checkbox" name="hobby" value="s">
drink<input type="checkbox" name="hobby" value="d">
play<input type="checkbox" name="hobby" value="p">
<br>
<input type="submit" value="login">
</form>
</body>
</html>
Map<String,String[]>
key value
---------------------------------
username {"sza002415"}
userpwd {"sadasda"}
hobby {"s","d","p"}
Map<String,String[]> parameterMap = request.getParameterMap();
Set<String> keys = parameterMap.keySet();
Iterator<String> it = keys.iterator();
while (it.hasNext()){
String key = it.next();
String[] values = parameterMap.get(key);
for (String value:values){
out.print(value + "<br>");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Enumeration<String> names = request.getParameterNames();
while (names.hasMoreElements()){
String name = names.nextElement();
out.print(name+"<br>");
}
String[] usernames = request.getParameterValues("username");
String[] userpwds = request.getParameterValues("userpwd");
String[] interests = request.getParameterValues("hobby");
for (String username : usernames){
out.print(username);
}
for (String pwd : userpwds){
out.print(pwd);
}
for (String interest :interests){
out.print(interest);
}
}
使用Servlet的转发机制
执行Aservlet之后,跳转到Bservlet。
1.获取请求转发器对象
2.调用请求转发器RequestDispatcher的forward方法
package sza_htmservlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class time extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Object sysTime = request.getAttribute("sysTime");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print(sysTime);
}
}
package sza_htmservlet;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
public class time_0 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Date nowtime = new Date();
request.setAttribute("sysTime",nowtime);
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/time");
requestDispatcher.forward(request,response);
}
}
两个servlet如何共享数据?
1.将数据放到servletContext当中,但是应用域范围太大,占用资源太多,不建议使用
2.放到request域当中要用时,使用转发机制
转发的下一个资源不一定是Servlet
比如html…
转发的路径以“/”开始,不加项目名
request.getRequestDispatcher("time.html").forward(request,response);
关于request对象中两个非常容易混淆的方法
String username = request.getParameter("username");
Object obj = request.getAttribute("name");
|