1.1通过getParamter(键名)来获取参数值 get和post请求通用
package cn.sc;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;
@WebServlet( "/test3")
public class test3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//通过getParamter(键名)来获取参数值 get和post请求通用
String username = request.getParameter("username");
String hobby = request.getParameter("hobby");
System.out.println("爱好是:"+hobby);
System.out.println(username);
}
JavaWeb代码
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<form action="test3" method="post">
用户名<input type="text" placeholder="请输入用户名" name="username"/><br/>
密码<input type="password" placeholder="请输入密码" name="pwd">
<input type="checkbox" value="cll" name="hobby">123456
<input type="checkbox" value="ckl" name="hobby">123457
<input type="submit" value="登陆"/>
form>
body>
html>
System.out.println("-------------通过键名获取所有的参数--------------");
// request.getParameterValues 适用于 一个键名 有多个参数
String[] hobies = request.getParameterValues("hoby");
for (String hoby1 : hobies) {
System.out.println(hoby1);
}
1.3request.getParamyterNames() 获取访问该servlet的所有的 键名
Enumeration<String> parameterNames = request.getParameterNames();
while (parameterNames.hasMoreElements()){
String name = parameterNames.nextElement();
// System.out.println(name);
String value = request.getParameter(name);
System.out.println(name+"+++"+value);
}
Map<String, String[]> pm = request.getParameterMap();
Set<String> keySet = pm.keySet();
for (String key : keySet) {
String[] values = pm.get(key);
for (String value : values) {
System.out.println(key+"----"+value);
}
}
我们在1.1方法中,username文本框若输入中文,则会出现乱码情况例如
是一个在服务器内部的资源跳转的方式,例如我们在世纪开发中,需要些不同的servlet,每个人完成不同的功能,当我们访问aservlet实现部分功能后需要跳转到一个新的bservlet里面,这就是资源跳转。
举个例子
package cn.sc;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/test1")
public class test1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("我的貂,蝉在哪里");
//1.获取request的请求转发器,内部参数为转发的路径
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/test2");
//2.调用转发器对象,传入参数request和response
requestDispatcher.forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
跳转到test2
package cn.sc;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/test2")
public class test2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("千军万马一将在,探囊取物有何难");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}