中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當前位置: 首頁 > news >正文

廣西玉林建設廳官方網(wǎng)站代運營公司哪家好一些

廣西玉林建設廳官方網(wǎng)站,代運營公司哪家好一些,傻瓜式網(wǎng)站開發(fā),鄭州官網(wǎng)網(wǎng)站推廣優(yōu)化公司Servlet 是在服務器上運行的小程序。這個詞是在 Java applet的環(huán)境中創(chuàng)造的,Java applet 是一種當作單獨文件跟網(wǎng)頁一起發(fā)送的小程序,它通常用于在客戶端運行,結果得到為用戶進行運算或者根據(jù)用戶互作用定位圖形等服務。服務器上需要一些程序…

Servlet 是在服務器上運行的小程序。這個詞是在 Java applet的環(huán)境中創(chuàng)造的,Java applet 是一種當作單獨文件跟網(wǎng)頁一起發(fā)送的小程序,它通常用于在客戶端運行,結果得到為用戶進行運算或者根據(jù)用戶互作用定位圖形等服務。

服務器上需要一些程序,常常是根據(jù)用戶輸入訪問數(shù)據(jù)庫的程序。這些通常是使用公共網(wǎng)關接口(Common Gateway Interface,CGI)應用程序完成的。然而,在服務器上運行 Java,這種程序可使用 Java 編程語言實現(xiàn)。在通信量大的服務器上,JavaServlet 的優(yōu)點在于它們的執(zhí)行速度更快于 CGI 程序。各個用戶請求被激活成單個程序中的一個線程,而無需創(chuàng)建單獨的進程,這意味著服務器端處理請求的系統(tǒng)開銷將明顯降低。

實現(xiàn)過程

最早支持 Servlet 技術的是 JavaSoft 的 Java Web Server。此后,一些其它的基于 Java 的 Web Server 開始支持標準的 Servlet API。Servlet 的主要功能在于交互式地瀏覽和修改數(shù)據(jù),生成動態(tài) Web 內容。這個過程為:

客戶端發(fā)送請求至服務器端;

服務器將請求信息發(fā)送至 Servlet;

Servlet 生成響應內容并將其傳給服務器。響應內容動態(tài)生成,通常取決于客戶端的請求;

服務器將響應返回給客戶端。

Servlet 看起來像是通常的 Java 程序。Servlet 導入特定的屬于 Java Servlet API 的包。因為是對象字節(jié)碼,可動態(tài)地從網(wǎng)絡加載,可以說 Servlet 對 Server 就如同 Applet對 Client 一樣,但是,由于 Servlet 運行于 Server 中,它們并不需要一個圖形用戶界面。從這個角度講,Servlet 也被稱為 FacelessObject。

一個 Servlet 就是 Java 編程語言中的一個類,它被用來擴展服務器的性能,服務器上駐留著可以通過“請求-響應”編程模型來訪問的應用程序。雖然 Servlet 可以對任何類型的請求產生響應,但通常只用來擴展 Web 服務器的應用程序。Servlet根據(jù)請求,動態(tài)的去生成要返回的html,并不是早就準備好的html文檔。

Tomcat 服務器是一個免費的開放源代碼的Web 應用服務器,屬于輕量級應用服務器,在中小型系統(tǒng)和并發(fā)訪問用戶不是很多的場合下被普遍使用,是開發(fā)和調試JSP 程序的首選。對于一個初學者來說,可以這樣認為,當在一臺機器上配置好Apache 服務器,可利用它響應HTML(標準通用標記語言下的一個應用)頁面的訪問請求。實際上Tomcat是Apache 服務器的擴展,但運行時它是獨立運行的,所以當你運行tomcat 時,它實際上作為一個與Apache 獨立的進程單獨運行的。

訣竅是,當配置正確時,Apache 為HTML頁面服務,而Tomcat 實際上運行JSP 頁面和Servlet。另外,Tomcat和IIS等Web服務器一樣,具有處理HTML頁面的功能,另外它還是一個Servlet和JSP容器,獨立的Servlet容器是Tomcat的默認模式。不過,Tomcat處理靜態(tài)HTML的能力不如Apache服務器

這里注意兩個問題:

  1. tomcat啟動亂碼問題

  1. tomcat的啟動成功的判斷方法

  1. 修改端口和調整亂碼問題

定位到服務配置文件中

定位到日志文件中

過程簡述:

1.首先從瀏覽器輸入網(wǎng)址發(fā)出請求,請求發(fā)送到tomcat

2.tomcat根據(jù)映射假名匹配,匹配到后找到真名

3.根據(jù)真名再匹配servlet的具體路徑

4.找到后Servlet處理請求,并且返回響應到瀏覽器

5.瀏覽器加載響應。

放在webapp目錄中的WEB-INF中的sub文件不能隨便被外部訪問,里面放html,是訪問不到的。

這樣就可以訪問了,一般推薦采用絕對路徑,不采用相對路徑。

注意上下文問題:就是servlet的映射的根路徑的控制www.localhost:8080/xxxxx/url-pattern

一般都設置為忽略不常用。

        String[] specs = request.getParameterValues("spec");PrintWriter out=response.getWriter();for(String spec:specs){out.println("<h2>spec:"+spec+"</h2>");}

以下是兩種請求的處理方式。

    @Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String name = req.getParameter("name");String mobile = req.getParameter("mobile");PrintWriter out = resp.getWriter();out.println("GET");out.println("<h1>"+name+"</h1>");out.println("<h1>"+mobile+"</h1>");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String name = req.getParameter("name");String mobile = req.getParameter("mobile");PrintWriter out = resp.getWriter();out.println("POST");out.println("<h1>"+name+"</h1>");out.println("<h1>"+mobile+"</h1>");}
public class FirstServlet extends HttpServlet {//第一階段裝載,讀取xml文件,加載配置信息,并未創(chuàng)建//第一次訪問(地址欄輸入)的時候才會創(chuàng)建servlet實體對象public FirstServlet() {System.out.println("正在創(chuàng)建...");}//創(chuàng)建之后初始化@Overridepublic void init() throws ServletException {System.out.println("正在初始化");}@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("正在處理。。");}//在tomcat關閉前會自動的關閉銷毀@Overridepublic void destroy() {System.out.println("正在銷毀");}
//    注意全局有且僅有一個servlet對象實例,有多個請求時,單例多線程。
}
在類上注解寫上不用配置web.xml了
@WebServlet("/anno")
public class AnnotationServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.getWriter().println("im annotation servlet!");}
}

Uniform Resource Locator

Uniform Resource Identifier

HTTP 協(xié)議的 Header 是一塊數(shù)據(jù)區(qū)域,分為請求頭和響應頭兩種類型,客戶端向服務區(qū)發(fā)送請求時帶的是請求頭,而服務器響應客戶端數(shù)據(jù)時帶的是響應頭。

請求頭里主要是客戶端的一些基礎信息,UA(user-agent)就是其中的一部分,而響應頭里是響應數(shù)據(jù)的一些信息,以及服務器要求客戶端如何處理這些響應數(shù)據(jù)的指令。請求頭里面的關鍵信息如下:

1) accept

表示當前瀏覽器可以接受的文件類型,假設這里有 image/webp,表示當前瀏覽器可以支持 webp 格式的圖片,那么當服務器給當前瀏覽器下發(fā) webp 的圖片時,可以更省流量。

2) accept-encoding

表示當前瀏覽器可以接受的數(shù)據(jù)編碼,如果服務器吐出的數(shù)據(jù)不是瀏覽器可接受的編碼,就會產生亂碼。

3) accept-language

表示當前使用的瀏覽語言。

4) Cookie

很多和用戶相關的信息都存在 Cookie 里,用戶在向服務器發(fā)送請求數(shù)據(jù)時會帶上。例如,用戶在一個網(wǎng)站上登錄了一次之后,下次訪問時就不用再登錄了,就是因為登錄成功的 token 放在了 Cookie 中,而且隨著每次請求發(fā)送給服務器,服務器就知道當前用戶已登錄。

5) user-agent

表示瀏覽器的版本信息。當服務器收到瀏覽器的這個請求后,會經(jīng)過一系列處理,返回一個數(shù)據(jù)包給瀏覽器,而響應頭里就會描述這個數(shù)據(jù)包的基本信息。

響應頭里的關鍵信息有:

1) content-encoding

表示返回內容的壓縮編碼類型,如“Content-Encoding :gzip”表示這次回包是以 gzip 格式壓縮編碼的,這種壓縮格式可以減少流量的消耗。

2) content-length

表示這次回包的數(shù)據(jù)大小,如果數(shù)據(jù)大小不匹配,要當作異常處理。

3) content-type

表示數(shù)據(jù)的格式,它是一個 HTML 頁面,同時頁面的編碼格式是 UTF-8,按照這些信息,可以正常地解析出內容。content-type 為不同的值時,瀏覽器會做不同的操作,如果 content-type 是 application/octet-stream,表示數(shù)據(jù)是一個二進制流,此時瀏覽器會走下載文件的邏輯,而不是打開一個頁面。

4) set-cookie

服務器通知瀏覽器設置一個 Cookie;通過 HTTP 的 Header,可以識別出用戶的一些詳細信息,方便做更定制化的需求,如果大家想探索自己發(fā)出的請求中頭里面有些什么,可以這樣做:打開 Chrome 瀏覽器并按“F12”鍵,喚起 Chrome 開發(fā)者工具,選擇 network 這個 Tab,瀏覽器發(fā)出的每個請求的詳情都會在這里顯示。

根據(jù)請求頭進行判斷是哪一種設備,就返回哪種設備的頁面。

通過請求頭判斷處理方式,這里有一句話要注意,設置編碼方式,否則中文字符串會亂碼?號。

@WebServlet("/ua")
public class UserAgentServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String ua = req.getHeader("User-Agent");String output = "";if(ua.toLowerCase().indexOf("window nt")!=-1){output = "<h1>這是PC頁面</h1>";}else {output = "<h1>這是移動端頁面";}resp.setContentType("text/html;charset=utf-8");PrintWriter out=resp.getWriter();out.println(output);}
}

401代表沒有登陸,403代表沒有權限

@WebServlet("/ua")
public class UserAgentServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String ua = req.getHeader("User-Agents");String output = "";if(ua==null){resp.getWriter().println("請求頭不存在");return;}if(ua.toLowerCase().indexOf("window nt")!=-1){output = "<h1>這是PC頁面</h1>";}else {output = "<h1>這是移動端頁面";}resp.setContentType("text/html;charset=utf-8");PrintWriter out=resp.getWriter();out.println(output);}
}
@WebServlet("/ct")
public class ContentTypeServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String output = "<h1><a href='http://www.baidu.com'><span>百度</span>";response.setContentType("aplication/x-msdownload;charset=utf-8");response.getWriter().println(output);}
}
@WebServlet("/index")
public class IndexServlet extends HttpServlet {@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");response.getWriter().println("我是主頁");}@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doPost(req,resp);}
}

@WebServlet("/login")
public class CheckLoginServlet extends HttpServlet {@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String username = request.getParameter("username");String password = request.getParameter("password");if(username.equals("admin")&&password.equals("123456")){System.out.println("登錄成功");//請求轉發(fā)//request.getRequestDispatcher("/index").forward(request,response);//響應重定向response.sendRedirect("/index");}else {System.out.println("用戶登錄失敗");}}
}
<body><form action="/login" method="post"><input name="username"><input name="password" type="password"><input type="submit"></form>
</body>

方式一請求轉發(fā)的形式

方式二響應重定向的方式

兩者的區(qū)別:

請求轉發(fā):一個是一次即可請求完成,緊密相關,因為由一個tomcat管理,只能在應用的內部進行轉發(fā)

響應重定向:不是緊密相連的對象,默認get方式,因為兩次響應,可以跳到其他服務器管理站點如百度

登錄后主頁面顯示賬號名稱實例:

@WebServlet("/login")
public class CheckLoginServlet extends HttpServlet {@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String username = request.getParameter("username");String password = request.getParameter("password");if(username.equals("admin")&&password.equals("123456")){System.out.println("登錄成功");request.setAttribute("username",username);request.setAttribute("password",password);
//            請求轉發(fā),總體一個請求,可以設置和獲取屬性request.getRequestDispatcher("/index").forward(request,response);
//            //響應重定向,一共兩個請求,會出現(xiàn)Null的情況
//            response.sendRedirect("/index");}else {System.out.println("用戶登錄失敗");}}
}
@WebServlet("/index")
public class IndexServlet extends HttpServlet {@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");String username = (String)request.getAttribute("username");response.getWriter().println("我是主頁"+username);}@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doPost(req,resp);}
}

Cookie

@WebServlet("/cookie/random")
public class RandomServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {Integer random = new Random().nextInt(10000);Cookie cookie = new Cookie("random" , String.valueOf(random));cookie.setMaxAge(60*60*24);response.addCookie(cookie);response.setContentType("text/html;charset=utf-8");response.getWriter().println("<h2>隨機數(shù)" +  random+ "已生成</h2>");}
}
@WebServlet("/cookie/show")
public class CookieServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {Cookie[] cookies = request.getCookies();Integer random=-1;if(cookies==null){response.getWriter().println("無cookie");return;}for(Cookie c:cookies){if (c.getName().equals("random")) {random=Integer.parseInt(c.getValue());}}response.setContentType("text/html;charset=utf-8");response.getWriter().println(random);}
}

為什么說session是和瀏覽器窗口綁定的?

因為當我把窗口關閉,cookie默認消除,此時向tomcat發(fā)送請求的時候,因為cookie沒了,所以此時會創(chuàng)建一個新的session,原來的30min會自動銷掉。

public class CheckLoginServlet extends HttpServlet {@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String username = request.getParameter("username");String password = request.getParameter("password");if(username.equals("admin")&&password.equals("123456")){System.out.println("登錄成功");//為請求開辟一片內存空間,并設置屬性及值,設置同時設置cookie的sessionidHttpSession session = request.getSession();session.setAttribute("username",username);//響應設置好cookie的sessionid,響應重定向到indexresponse.sendRedirect("/index");}else {System.out.println("用戶登錄失敗");}}
}
@WebServlet("/index")
public class IndexServlet extends HttpServlet {@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");//根據(jù)cookie來查找session空間,并獲取屬性HttpSession session = request.getSession();String username=(String) session.getAttribute("username");response.getWriter().println("我是主頁"+username);}@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doPost(req,resp);}
}
@WebServlet("/servletcontext/init")
public class ServletContextInitServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {ServletContext servletContext = req.getServletContext();servletContext.setAttribute("copyright","xxxx");resp.setContentType("text/html;charset=utf-8");resp.getWriter().println("ServletContext已初始化");}
}
@WebServlet("/servletcontext/indx")
public class IndexServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.setContentType("text/html;charset=utf-8");ServletContext context = req.getServletContext();String copyright = (String)context.getAttribute("copyright");resp.getWriter().println(copyright);}
}

http://www.risenshineclean.com/news/50777.html

相關文章:

  • 各大網(wǎng)站提交入口深圳網(wǎng)站建設開發(fā)公司
  • 網(wǎng)站制作的評價指標中手機網(wǎng)頁設計
  • 為公益組織做網(wǎng)站bing搜索引擎入口
  • 淘寶上做網(wǎng)站可靠嗎網(wǎng)站改版
  • 網(wǎng)站建設在家兼職做網(wǎng)絡營銷畢業(yè)論文8000字
  • 哪個網(wǎng)站可以接活做網(wǎng)建公司
  • 廣東商城網(wǎng)站建設公司網(wǎng)站創(chuàng)建免費用戶
  • 廣州網(wǎng)站建設推廣公司哪家好怎樣創(chuàng)建網(wǎng)站平臺
  • 北京網(wǎng)站制作培訓seo關鍵詞推廣方式
  • 項目網(wǎng)絡圖最早開始時間seo推廣的特點
  • 上海找做網(wǎng)站公司好寧波網(wǎng)絡推廣
  • java在線編程網(wǎng)站廣告推廣策劃方案
  • 網(wǎng)頁制作及網(wǎng)站建設seo站長工具 論壇
  • 在哪個網(wǎng)做免費網(wǎng)站好百度客服中心人工在線電話
  • 昆明北京網(wǎng)站建設網(wǎng)絡營銷的五個發(fā)展階段
  • 服務好的網(wǎng)站建設聯(lián)系人市場營銷策略有哪4種
  • 幾項措施政府網(wǎng)站集約化建設公司網(wǎng)絡推廣營銷
  • eclipse 簡單網(wǎng)站開發(fā)高級搜索入口
  • 中國建設電工立網(wǎng)站網(wǎng)店代運營收費
  • 給菠菜網(wǎng)站做外包網(wǎng)絡營銷簡介
  • 北京好的網(wǎng)站制作線上宣傳渠道
  • 上海服裝品牌網(wǎng)站建設網(wǎng)站開通
  • 免費訪問國外網(wǎng)站的app站長之家whois查詢
  • 做游戲的外包網(wǎng)站seo網(wǎng)絡優(yōu)化是做什么的
  • 企業(yè)網(wǎng)站必須備案網(wǎng)站按天扣費優(yōu)化推廣
  • 東莞網(wǎng)站建設設計公司教育培訓學校
  • 網(wǎng)站開發(fā)流程及進度安排寧波網(wǎng)站關鍵詞優(yōu)化排名
  • 公司介紹視頻制作網(wǎng)站優(yōu)化排名金蘋果系統(tǒng)
  • 西安網(wǎng)上注冊公司百度快速優(yōu)化軟件
  • 視覺設計網(wǎng)站推薦世界球隊最新排名