慕课网Servlet与JSP进阶笔记
加油吧
1.请求结构
2.响应结构
3.HTTP常见状态码、ContentType的作用
4.请求转发与重定向
1 //实现了请求转发的功能,产生一次请求 2 request.getRequestDispatcher("/direct/index").forward(request,response); 3 4 //响应重定向需要增加contextPath,产生两次请求 5 response.sendRedirect("/servlet_advanced/direct/index");
1 request.setAttribute("username","admin"); 2 //返回的对象是Object对象,需要强制转化成String 3 String username=(String)request.getAttribute("username");
5.浏览器Cookie、Session-用户会话
1 2 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 3 // TODO Auto-generated method stub 4 System.out.println("用户登录成功"); 5 Cookie cookie=new Cookie("user","admin"); 6 cookie.setMaxAge(60*60*24*7); 7 response.addCookie(cookie); 8 response.getWriter().println("login success"); 9 } 10 11 }
1 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 2 // TODO Auto-generated method stub 3 Cookie[] cs=request.getCookies(); 4 if(cs==null) { 5 response.getWriter().println("user not login"); 6 return; 7 } 8 String user=null; 9 for(Cookie c:cs) { 10 System.out.println(c.getName()+":"+c.getValue()); 11 if(c.getName().equals("user")) { 12 user=c.getValue(); 13 break; 14 } 15 } 16 if(user==null) { 17 response.getWriter().println("user not login"); 18 }else { 19 response.getWriter().println("user : "+user ); 20 } 21 }
1 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 2 // TODO Auto-generated method stub 3 System.out.println("用户登录成功"); 4 //获取到用户会话对象 5 HttpSession session=request.getSession(); 6 String sessionId=session.getId(); 7 System.out.println(sessionId); 8 session.setAttribute("name", "张三"); 9 request.getRequestDispatcher("/session/index").forward(request,response); 10 11 } 12 13 }
1 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 2 // TODO Auto-generated method stub 3 HttpSession session=request.getSession(); 4 String sessionId=session.getId(); 5 System.out.println(sessionId); 6 String name=(String)session.getAttribute("name"); 7 response.setContentType("text/html;charset=utf-8"); 8 response.getWriter().println("这是首页,当前用户为:"+name); 9 } 10 }
6.ServletContext
1 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 2 // TODO Auto-generated method stub 3 ServletContext context=request.getServletContext(); 4 String copyright=context.getInitParameter("copyright"); 5 String title=context.getInitParameter("title"); 6 context.setAttribute("copyright",copyright ); 7 context.setAttribute("title", title); 8 response.getWriter().println("init success"); 9 } 10 }
1 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 2 // TODO Auto-generated method stub 3 //请求体乱码解决,是在doPost里面填入方法request.setCharacterEncoding("UTF-8"); 4 request.setCharacterEncoding("UTF-8"); 5 ServletContext context=request.getServletContext(); 6 String copyright=(String)context.getAttribute("copyright"); 7 String title=(String)context.getAttribute("title"); 8 response.setContentType("text/html;charset=utf-8"); 9 response.getWriter().println(""+title+"
"+copyright); 10 } 11 12 }
Java Web三大作用域对象:从上到下,作用域递增
7.Web应用的中文乱码问题
1 //请求体乱码解决,是在doPost里面填入方法request.setCharacterEncoding("UTF-8"); 2 //对于Tomcat8.x的版本,默认doGet,get请求发送中文就是UTF-8的格式,因此无需转换无需配置 3 request.setCharacterEncoding("UTF-8"); 4 response.setContentType("text/html;charset=utf-8");
8.web.xml常用配置
String id=url.substring(url.lastIndexOf("/")+1),截取id
1 xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> 3 <display-name>servlet_advanceddisplay-name> 4 <welcome-file-list>
5 <welcome-file>index.htmlwelcome-file> 6 <welcome-file>index.htmwelcome-file> 7 <welcome-file>index.jspwelcome-file> 8 <welcome-file>default.htmlwelcome-file> 9 <welcome-file>default.htmwelcome-file> 10 <welcome-file>default.jspwelcome-file> 11 welcome-file-list> 12 13 <context-param> 14 <param-name>copyrightparam-name> 15 <param-value>2020 学习param-value> 16 context-param> 17 <context-param> 18 <param-name>titleparam-name> 19 <param-value>加油加油加油param-value> 20 context-param> 21 22 <error-page> 23 <error-code>404error-code> 24 <location>/error/404.htmllocation> 25 error-page> 26 <error-page> 27 <error-code>500error-code> 28 <location>/error/500.htmllocation> 29 error-page> 30 web-app>
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
head>
<body>
资源不存在
body>
html>
9.JSP九大内置参数
10.Tomcat常用配置、Java Web打包与发布
打包
发布
练习
1 package servlet.c; 2 3 import java.io.IOException; 4 import java.util.HashMap; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 /** 13 * Servlet implementation class p 14 */ 15 @WebServlet("/p") 16 public class p extends HttpServlet { 17 private static final long serialVersionUID = 1L; 18 19 /** 20 * @see HttpServlet#HttpServlet() 21 */ 22 public p() { 23 super(); 24 // TODO Auto-generated constructor stub 25 } 26 27 /** 28 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 29 */ 30 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 31 // TODO Auto-generated method stub 32 response.setContentType("text/html;charset=utf-8"); 33 String key=request.getParameter("key"); 34 HashMaphas=new HashMap(); 35 has.put("apple","苹果"); 36 has.put("banana","香蕉"); 37 has.put("organe","橘子"); 38 has.put("猪婆","沛维"); 39 has.put("臭猪","还是沛维"); 40 boolean b=has.containsKey(key); 41 if(b) { 42 response.getWriter().println(" "+has.get(key)+"
"); 43 }else { 44 request.getRequestDispatcher("/c2.jsp").forward(request, response); 45 } 46 47 } 48 49 /** 50 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 51 */ 52 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 53 // TODO Auto-generated method stub 54 doGet(request, response); 55 } 56 57 }
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="utf-8"> 7 <title>Insert title heretitle> 8 head> 9 <body> 10 <form action="/servlet_advanced/p" method="get"> 11 <input type="text" name="key" placeholder="请输入要查询的单词"/> 12 <input type="submit" value="查询"/> 13 form> 14 body> 15 html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="utf-8"> 7 <title>Insert title heretitle> 8 head> 9 <body> 10 <h1 style="color:red">没有找到对应单词的解释!h1> 11 body> 12 html>