/*页首的跳转连接,*/

Servlet与JSP进阶

慕课网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("<h1>"+title+"</h1>"+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_advanced</display-name> 4 <welcome-file-list>
5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 <!-- context-param配置全局参数信息 --> 13 <context-param> 14 <param-name>copyright</param-name> 15 <param-value>2020 学习</param-value> 16 </context-param> 17 <context-param> 18 <param-name>title</param-name> 19 <param-value>加油加油加油</param-value> 20 </context-param> 21 <!-- 指定错误页面 --> 22 <error-page> 23 <error-code>404</error-code> 24 <location>/error/404.html</location> 25 </error-page> 26 <error-page> 27 <error-code>500</error-code> 28 <location>/error/500.html</location> 29 </error-page> 30 </web-app>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</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         HashMap<String,String> has=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("<h1 style='color:blue'>"+has.get(key)+"</h1>");
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 here</title>
 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 here</title>
 8 </head>
 9 <body>
10 <h1 style="color:red">没有找到对应单词的解释!</h1>
11 </body>
12 </html>

 

 

posted @ 2020-02-11 14:50  奇奇锋  阅读(150)  评论(0)    收藏  举报
/* 看板娘 */