目录
- ServletRequest常用方法
- getParameter()(获取单个值)
- getParameterValues()(获取相同过name的值集合)
- getParameterMap()(获取键值对集合)
- setAttribute()(将值存储至域)
- getAttribute()(从域中读取值)
- getRequestDispatcher()(转发)
- getMethod()(获取请求方式)
- getRequesURI()(获取主机名到请求参数之间的路径)
- getRequestURL()(获取完整请求路径)
- getContextPath()(获取web项目根路径)
- getServletPath()(获取请求URI中的Servlet路径的字符串)
- getscheme()(获取当前请求所使用的协议)
- getServerName()(获取当前请求的服务器名称)
- getServerPort()(获取当前请求的服务器端口)
- getWriter() 获取响应的打印输出流对象
- sendRedirect() 重定向
- s
- 总结
ServletRequest常用方法
getParameter()(获取单个值)
接收类型:String
收到页面提交的单个值,字符串
例:
String username = req.getParameter(“username”);
String passwprd = req.getParameter(“password”);
/*
* getParameter收到的是单个值(指定名称)
*
* */
@WebServlet("/login")
public class UserLogin extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String username = req.getParameter("username");
//req形参中保存了我们传过来的username,password
//我们需要用String 变量名 通过getParameter方法来接收
String passwprd = req.getParameter("password");
if ("admin".equals(username) && "123456".equals(passwprd)) {
System.out.println("登录成功");
}else {
System.out.println("登录失败");
}
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>用户登录</h1>
<form action="login" method="post">
<label>用户名:<input type="text" name="username"></label>
<label>密码:<input type="password" name="password"></label>
<input type="submit" value="登录">
</form>
</body>
</html>
getParameterValues()(获取相同过name的值集合)
接收类型:String[]
收到页面提交相同名称参数值,存为数组
例:
String[] hobbys = req.getParameterValues(“hobby”);
/*
*
* getParameterValues收到页面提交相同名称参数值,存为数组
*
* */
@WebServlet("/login")
public class UserLogin extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
req.setCharacterEncoding("utf-8"); //处理乱码
//我们需要用String[] 变量名 通过getParameterValues方法来接收
String[] hobbys = req.getParameterValues("hobby");
if (hobbys!=null) {
for (String hobby : hobbys) {
System.out.println(hobby);
}
}
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>爱好</h1>
<form action="login" method="post">
<input type="checkbox" name="hobby" value="唱歌">唱歌
<input type="checkbox" name="hobby" value="跑步">跑步
<input type="checkbox" name="hobby" value="跳绳">跳绳
<input type="submit" value="提交">
</form>
</body>
</html>
getParameterMap()(获取键值对集合)
接收类型:Map<String, String[]>
//获取所有参数将以为map集合形式返回
例:
Map<String, String[]> map = req.getParameterMap();
/*
* Map
* keySet方法可以获取到对应的keyname,
* get方法可以获取到对应keyname的value
* */
@WebServlet("/user_02")
public class UserServlet_02 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp); //当提交方式为get方式时候,自动转成post方式
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
//获取所有参数将以为map参数形式返回
Map<String, String[]> map = req.getParameterMap();
//使用Map的ketSet()方法获取map里的值并存为集合
Set<String> keySet = map.keySet();
//遍历keySet集合,声明key遍历预接收keySet集合里的每个值
for (String key : keySet) {
System.out.println(key+"对应的值");
//values接收key对应的value值
String[] values = map.get(key);
for (String value : values) {
System.out.println(value);
}
System.out.println("-------------------我是一个分隔符-------------");
}
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="user_02" method="get">
姓名:<input type="text" name="name"><br>
年龄:<input type="text" name="age"><br>
爱好:<br>
<input type="checkbox" name="hobby" value="抽烟">抽烟
<input type="checkbox" name="hobby" value="喝酒">喝酒
<input type="checkbox" name="hobby" value="烫头">烫头
<br>
<input type="submit" value="提交">
</form>
</body>
</html>
setAttribute()(将值存储至域)
将键值对存储至HttpServletRequest域中
与getAttribute(读取)配对使用
例:
req.setAttribute(“username”, “admin”); //存入域中
Object username = req.getAttribute(“username”); //通过username读取域中信息
System.out.println(username); //打印
getAttribute()(从域中读取值)
从HttpServletRequest域中读取值
例:
req.setAttribute(“username”, “admin”); //存入域中
Object username = req.getAttribute(“username”); //通过username读取域中信息
System.out.println(username); //打印
getRequestDispatcher()(转发)
将域信息转发到其他class类中
例:
req.getRequestDispatcher(“user_03”).forward(req, resp);
//转发到/user_03
getMethod()(获取请求方式)
接收类型:String
获取页面提交请求的方式 get? or post?
public class getMethod extends HelloServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
//获取页面提交请求的方式 get? or post?
String method = req.getMethod();
System.out.print(method);
}
}
getRequesURI()(获取主机名到请求参数之间的路径)
接收类型:String
public class getRequesURI extends HelloServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
//获取返回主机名到请求参数之间的路径
String requestURI = req.getRequestURI();
System.out.print(requestURI);
}
}
getRequestURL()(获取完整请求路径)
接收类型:String
public class getRequestURL extends HelloServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
//获取完整的请求路径
StringBuffer requestURL = req.getRequestURL();
System.out.print(requestURL);
}
}
getContextPath()(获取web项目根路径)
接收类型:String
public class getContextPath extends HelloServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
//获取web项目根路径
String contextpath = req.getContextPath();
System.out.print(contextpath);
}
}
getServletPath()(获取请求URI中的Servlet路径的字符串)
接收类型:String
public class getServletPath extends HttpServlet{
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
//获取请求URI中的Servlet路径的字符串
String servletPath = req.getServletPath();
System.out.print(servletPath);
}
}
getscheme()(获取当前请求所使用的协议)
接收类型:String
public class getscheme extends HttpServlet{
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
//获取当前请求所使用的协议
String scheme = req.getScheme();
System.out.print(scheme);
}
}
getServerName()(获取当前请求的服务器名称)
接收类型:String
public class getServerName extends HttpServlet{
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
//获取当前请求的服务器名称
String servername = req.getServerName()();
System.out.print(servername);
}
}
getServerPort()(获取当前请求的服务器端口)
接收类型:int
public class getServerPort extends HttpServlet{
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
//获取当前请求的服务器端口
int serverport = req.getServerPort()();
System.out.print(serverport);
}
}
getWriter() 获取响应的打印输出流对象
@webServlet("/hello")
public class getWriter extends HttpServlet{
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
//处理乱码
resp.setContentType("text/html;charset=utf-8");
//响应给浏览器数据
//使用PrintWriter关键字声明变量writer来代表resp.getWriter()方法
PrintWriter writer = resp.getWriter();
//此处可理解为 resp.getWriter.print()
writer.print("我是响应到浏览器的数据");
}
}
sendRedirect() 重定向
@webServlet("/hello")
public class sendRedirect extends HttpServlet{
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//重定向到百度https:\\www.baidu.com
resp.sendRedirect("https:\\www.baidu.com");
}
}
s
@webServlet("/login")
public class LoginServlet extends HttpServlet{
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取请求参数
String username = req.getParamenter("username");
String password = req.getParamenter("password");
//密码判断
if("admin".equals(username) && "123456".equals(password)){
//登陆成功,跳转首页baidu(转发)
resp.getRequestDispatcher("https:\\www.baidu.com").forward(req,resp);
}else{
//登陆失败,跳转首页360(重定向)
resp.sendRedirect("https:\\www.360.com");
}
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>登录</h1>
<form action="login" method="post">
用户名:<input type="text" name="username"><br>
密 码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
总结
- Java Servlet又称作Java服务器小程序,运行在服务端,主要用于Web应用中的请求响应,逻辑处理功能,让JSP更专注于数据展示
- Servlet的实现步骤为:创建Servlet并且完成请求处理、在web.xml中配置Servlet及请求路径、页面上请求Servlet
- Servlet生命周期包括:加载实例化、初始化、服务、销毁几个阶段
- HttpServletRequest代表请求对象,可以调用其方法获取请求相关的信息,如请求的参数等
- HttpServletResonse代表响应对象,可以调用其方法实现响应功能,如重定向、设置响应编码等