1. 程式人生 > 實用技巧 >vue的繫結屬性v-bind

vue的繫結屬性v-bind

web容器在啟動的時候,它會為每一個web程式建立一個對應的ServletContext物件,它代表了當前的web應用;

1、共享資料

我在這個Servlet中儲存的資料,可以在另外一個servlet中拿到;

 1 package com.kuang.servlet;
 2 
 3 import javax.servlet.ServletContext;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.http.HttpServlet;
 6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse; 8 import java.io.IOException; 9 10 public class HelloServlet extends HttpServlet { 11 @Override 12 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 13 14 //this.getInitParameter() 初始化引數
15 //this.getServletConfig() Servlet配置 16 //this.getServletContext() Servlet上下文 17 ServletContext context = this.getServletContext(); 18 19 String username = "王劉"; //資料 20 context.setAttribute("username",username); //將一個數據儲存在了ServletContext中,名字為:username,值username
21 22 23 } 24 25 @Override 26 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 27 doGet(req, resp); 28 } 29 }

獲取資料:

 1 package com.kuang.servlet;
 2 
 3 import javax.servlet.ServletContext;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.http.HttpServlet;
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 import java.io.IOException;
 9 
10 public class GetServlet extends HttpServlet {
11     @Override
12     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
13         ServletContext context = this.getServletContext();
14         String username = (String) context.getAttribute("username");
15 
16         resp.setContentType("text/html");
17         resp.setCharacterEncoding("utf-8");
18         resp.getWriter().print("名字:"+username);
19     }
20 
21     @Override
22     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
23         doGet(req, resp);
24     }
25 }

註冊:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
 5                       http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
 6          version="4.0"
 7          metadata-complete="true">
 8 
 9     <servlet>
10         <servlet-name>hello</servlet-name>
11         <servlet-class>com.kuang.servlet.HelloServlet</servlet-class>
12     </servlet>
13     <servlet-mapping>
14         <servlet-name>hello</servlet-name>
15         <url-pattern>/hello</url-pattern>
16     </servlet-mapping>
17 
18     <servlet>
19         <servlet-name>getc</servlet-name>
20         <servlet-class>com.kuang.servlet.GetServlet</servlet-class>
21     </servlet>
22     <servlet-mapping>
23         <servlet-name>getc</servlet-name>
24         <url-pattern>/getc</url-pattern>
25     </servlet-mapping>
26 
27 
28 </web-app>