servlet
Request
1.getParameter()
获取请求参数通用方式:不论get还是post请求方式都可以使用下列方法来获取请求参数
1.String getParameter(String name):根据参数名称获取参数值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册页面</title>
</head>
<body>
<form action="/requestDemo6" method="get">
<input type="text" placeholder="请输入用户名" name="username"><br>
<input type="text" placeholder="请输入密码" name="password"><br>
<input type="submit" value="注册">
</form>
</body>
</html>
@WebServlet("/requestDemo6")
public class RequestDemo4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//根据参数名称获取参数值
String username = request.getParameter("username");
System.out.println("post");
System.out.println(username);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//根据参数名称获取参数值
String username = request.getParameter("username");
System.out.println("get");
System.out.println(username);
}
}
post
lisi
get
zhangsangetParameterValues
2.getParameterValues()
getParameterValues(String name):根据参数名称获取参数值的数组
<body>
<form action="/requestDemo6" method="get">
<input type="text" placeholder="请输入用户名" name="username"><br>
<input type="text" placeholder="请输入密码" name="password"><br>
<input type="checkbox" name="hobby" value="game">游戏
<input type="checkbox" name="hobby" value="study">学习<br>
<input type="submit" value="注册">
</form>
</body>
@WebServlet("/requestDemo6")
public class RequestDemo4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//根据参数名称获取参数值的数组
String[] hobbies = request.getParameterValues("hobby");
for (String hobby : hobbies) {
System.out.println(hobby);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
game
study
3.getParameterNames()
Enumeration getParameterNames():获取所有请求的参数名称
<body>
<form action="/requestDemo6" method="get">
<input type="text" placeholder="请输入用户名" name="username"><br>
<input type="text" placeholder="请输入密码" name="password"><br>
<input type="checkbox" name="hobby" value="game">游戏
<input type="checkbox" name="hobby" value="study">学习<br>
<input type="submit" value="注册">
</form>
</body>
@WebServlet("/requestDemo6")
public class RequestDemo4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取所有请求参数的名称
Enumeration<String> parameterNames = request.getParameterNames();
while (parameterNames.hasMoreElements()){
String name = parameterNames.nextElement();
System.out.println(name);
String value = request.getParameter(name);
System.out.println(value);
System.out.println("------------");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
username
lisi
------------
password
123
------------
hobby
game
------------
4.getParameterMap()
Map getParameterMap():获取所有参数的map集合
<body>
<form action="/requestDemo6" method="get">
<input type="text" placeholder="请输入用户名" name="username"><br>
<input type="text" placeholder="请输入密码" name="password"><br>
<input type="checkbox" name="hobby" value="game">游戏
<input type="checkbox" name="hobby" value="study">学习<br>
<input type="submit" value="注册">
</form>
</body>
@WebServlet("/requestDemo6")
public class RequestDemo4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//根据map集合获取所有的名称以及值
Map<String, String[]> parameterMap = request.getParameterMap();
//遍历
Set<String> keySet = parameterMap.keySet();
for (String name : keySet) {
//获取键获取值
String[] values = parameterMap.get(name);
System.out.println(name);
for (String value : values) {
System.out.println(value);
}
System.out.println("------------------");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
username
lisi
------------------
password
11111
------------------
hobby
game
study
------------------
中文乱码问题
中文乱码问题: get方式:tomcat 8 已经将get方式乱码问题解决了
post方式:会乱码
解决:在获取参数前,设置request的编码request.setCharacterEncoding(“utf-8”);
<body>
<form action="/requestDemo7" method="post">
<input type="text" placeholder="请输入用户名" name="username"><br>
<input type="text" placeholder="请输入密码" name="password"><br>
<input type="checkbox" name="hobby" value="game">游戏
<input type="checkbox" name="hobby" value="study">学习<br>
<input type="submit" value="注册">
</form>
</body>
@WebServlet("/requestDemo7")
public class RequestDemo7 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置中文乱码问题
request.setCharacterEncoding("utf-8");
//获取请求参数
String username = request.getParameter("username");
System.out.println(username);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
张三
请求转发
请求转发:一种在服务器内部的资源跳转方式
步骤:1. 通过request对象获取请求转发器对象:RequestDispatcher getRequestDispatcher(String path)
2. 使用RequestDispatcher对象来进行转发:forward(ServletRequest request, ServletResponse response)
3. 使用链式编程:request.getRequestDispatcher(String path).forward(request,response);
特点:1. 浏览器地址栏路径不发生变化
2. 只能转发到当前服务器内部资源中。
3. 转发是一次请求
@WebServlet("/requestDemo9")
public class RequestDemo9 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo9被访问了。。。。。");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response)