基础方法
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
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) {
e.printStackTrace();
}
}else if ("/logout".equals(pathInfo)) {
try {
this.logout(req, resp);
} catch (Exception e) {
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/>
密 码:<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();
System.out.println(header+":"+req.getHeader(header));
}
}