request内置对象

该博客主要介绍了Servlet的基础方法,包括获取请求头信息、处理GET请求、实现伪登录和注销功能。通过示例展示了如何处理中文乱码、获取请求参数、路径信息以及业务层数据的处理。此外,还提供了登录和注销的实现细节,如根据用户名和密码验证登录,以及登录成功后的页面跳转。
摘要由CSDN通过智能技术生成
基础方法
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//获取浏览器的信息
		//System.out.println("浏览器信息:"+req.getHeader("User-Agent"));
		//取得额外路径
		//System.out.println("额外路径:"+req.getPathInfo());//null
		//System.out.println("请求上下文路径"+req.getContextPath());//根路径
		System.out.println("传递的参数:"+req.getQueryString());
		System.out.println("url地址"+req.getRequestURL());
		System.out.println("urI地址"+req.getRequestURI());
		System.out.println("servlet的映射路径:"+req.getServletPath());
		System.out.println("项目部署的真实路径:"+req.getRealPath("/"));
		System.out.println("主机名称:"+req.getRemoteHost());
		
	}
其他方法
public class EmpServlet extends HttpServlet{
	//取得业务层实现类对象
	private IEmpService empService = (IEmpService)ServiceFactory.getInstance(EmpServicelmpl.class);
	
	@Override
	public void init() throws ServletException {
		System.out.println("初始化servlet");
	}
	@Override
	public void destroy() {
		System.out.println("销毁servlet阶段");
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
		//处理中文乱码
				req.setCharacterEncoding("utf-8");
				
	try {
		req.setAttribute("emp", this.empService.findAllSplit("A", 1, 10));
	} catch (Exception e) {
		e.printStackTrace();
		}
	System.out.println(req.getAttribute("emp"));//取得数据
	}
}

实现伪登录和注销
package com.biubiu.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.biubiu.factory.ServiceFactory;
import com.biubiu.service.IEmpService;
import com.biubiu.service.impl.EmpServicelmpl;
import com.biubiu.vo.Emp;

public class EmpServlet extends HttpServlet{
	//取得业务层实现类对象
	private IEmpService empService = (IEmpService)ServiceFactory.getInstance(EmpServicelmpl.class);
	
	@Override
	public void init() throws ServletException {
		System.out.println("初始化servlet");
	}
	@Override
	public void destroy() {
		System.out.println("销毁servlet阶段");
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//处理中文乱码
				req.setCharacterEncoding("utf-8");
				String pathInfo = req.getPathInfo();
				System.out.println(pathInfo);
				if ("/login".equals(pathInfo)) {
					try {
						this.login(req, resp);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else if ("/logout".equals(pathInfo)) {
					try {
						this.logout(req, resp);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

	}
	private void login(HttpServletRequest req,HttpServletResponse resp) throws Exception{
		String name=req.getParameter("username");
		String pwd = req.getParameter("pwd");
		if ("smith".equals(name)&&"1234".equals(pwd)) {
			System.out.println("登录成功");
			//跳转到欢迎页面或者首页(客户端跳转)
			resp.sendRedirect("/MvcProject/Pages/welcome.html");
		}else {
			System.out.println("用户名或者密码不正确");
			//服务器跳转到登录页面
			req.getRequestDispatcher("/Pages/index.html").forward(req, resp);
		}
	}
	private void logout(HttpServletRequest req,HttpServletResponse resp) throws Exception{
		System.out.println("注销用户");
		//跳转登录页面
		resp.sendRedirect("/MvcProject/Pages/index.html");
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);
		
	}
}

在这里插入图片描述

<!DOCTYPE html>
<html>
<head>
	<base href="/MvcProject/"/>
	<meta charset="utf-8">
	<title></title>
</head>
<body>
	<form action="emp/login" method="POST">
		<fieldset>
			用户名:<input type="text" name="username" value="张三"><br/><br/>&nbsp;&nbsp;码:<input type="password" name="pwd" value="1234"><br/>
			<input style="margin-left: 60px" type="submit" value="提交">
			<input type="reset" value="重置">
		</fieldset>
	</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
	<base href="/MvcProject/"/>
	<meta charset="utf-8">
	<title></title>
</head>
<body>
	<h1>欢迎光临</h1>
	<h3><a href="emp/logout">注销用户</a></h3>
</body>
</html>
获取请求头信息
@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//获取所有的请求头信息
		Enumeration<String> headers = req.getHeaderNames();
		while (headers.hasMoreElements()) {
			String header = (String) headers.nextElement();
			//取得对应的value值
			System.out.println(header+":"+req.getHeader(header));	
		}
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值