成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

servlet系列-Tomcat、webapp、servlet初步

tuantuan / 1498人閱讀

摘要:編譯范圍依賴在所有的中可用,同時(shí)它們也會(huì)被打包。已提供范圍依賴只有在當(dāng)或者一個(gè)容器已提供該依賴之后才使用。它們不是傳遞性的,也不會(huì)被打包。如果你將一個(gè)依賴范圍設(shè)置成系統(tǒng)范圍,你必須同時(shí)提供一個(gè)元素。

關(guān)于服務(wù)器 WebServer & Application Server

Q: What is the difference between an application server and a Web server?

A:

Web server exclusively handles HTTP requests, whereas an application server serves business logic to application programs through any number of protocols.

The Web server

https://www.javaworld.com/art...

Web server handles the HTTP protocol. When the Web server receives an HTTP request, it responds with an HTTP response, such as sending back an HTML page. To process a request, a Web server may respond with a static HTML page or image, send a redirect, or delegate the dynamic response generation to some other program such as CGI scripts, JSPs (JavaServer Pages), servlets, ASPs (Active Server Pages), server-side JavaScripts, or some other server-side technology. Whatever their purpose, such server-side programs generate a response, most often in HTML, for viewing in a Web browser.

Understand that a Web server"s delegation model is fairly simple. When a request comes into the Web server, the Web server simply passes the request to the program best able to handle it. The Web server doesn"t provide any functionality beyond simply providing an environment in which the server-side program can execute and pass back the generated responses. The server-side program usually provides for itself such functions as transaction processing, database connectivity, and messaging.

While a Web server may not itself support transactions or database connection pooling, it may employ various strategies for fault tolerance and scalability such as load balancing, caching, and clustering—features oftentimes erroneously assigned as features reserved only for application servers.

The application server

As for the application server, according to our definition, an application server exposes business logic to client applications through various protocols, possibly including HTTP. While a Web server mainly deals with sending HTML for display in a Web browser, an application server provides access to business logic for use by client application programs. The application program can use this logic just as it would call a method on an object (or a function in the procedural world).

Such application server clients can include GUIs (graphical user interface) running on a PC, a Web server, or even other application servers. The information traveling back and forth between an application server and its client is not restricted to simple display markup. Instead, the information is program logic. Since the logic takes the form of data and method calls and not static HTML, the client can employ the exposed business logic however it wants.

In most cases, the server exposes this business logic through a component API, such as the EJB (Enterprise JavaBean) component model found on J2EE (Java 2 Platform, Enterprise Edition) application servers. Moreover, the application server manages its own resources. Such gate-keeping duties include security, transaction processing, resource pooling, and messaging. Like a Web server, an application server may also employ various scalability and fault-tolerance techniques.

servlet


    javax.servlet
    javax.servlet-api
    3.1.0
    provided
插播一段maven scope的知識(shí)

compile (編譯范圍)

compile是默認(rèn)的范圍;如果沒有提供一個(gè)范圍,那該依賴的范圍就是編譯范圍。編譯范圍依賴在所有的classpath 中可用,同時(shí)它們也會(huì)被打包。

provided (已提供范圍)

provided 依賴只有在當(dāng)JDK 或者一個(gè)容器已提供該依賴之后才使用。例如, 如果你開發(fā)了一個(gè)web 應(yīng)用,你可能在編譯 classpath 中需要可用的Servlet API 來編譯一個(gè)servlet,但是你不會(huì)想要在打包好的WAR 中包含這個(gè)Servlet API;這個(gè)Servlet API JAR 由你的應(yīng)用服務(wù)器或者servlet 容器提供。已提供范圍的依賴在編譯classpath (不是運(yùn)行時(shí))可用。它們不是傳遞性的,也不會(huì)被打包。

runtime (運(yùn)行時(shí)范圍)

runtime 依賴在運(yùn)行和測(cè)試系統(tǒng)的時(shí)候需要,但在編譯的時(shí)候不需要。比如,你可能在編譯的時(shí)候只需要JDBC API JAR,而只有在運(yùn)行的時(shí)候才需要JDBC
驅(qū)動(dòng)實(shí)現(xiàn)。

test (測(cè)試范圍)

test范圍依賴 在一般的編譯和運(yùn)行時(shí)都不需要,它們只有在測(cè)試編譯和測(cè)試運(yùn)行階段可用。

system (系統(tǒng)范圍)

system范圍依賴與provided 類似,但是你必須顯式的提供一個(gè)對(duì)于本地系統(tǒng)中JAR 文件的路徑。這么做是為了允許基于本地對(duì)象編譯,而這些對(duì)象是系統(tǒng)類庫的一部分。這樣的構(gòu)件應(yīng)該是一直可用的,Maven 也不會(huì)在倉庫中去尋找它。如果你將一個(gè)依賴范圍設(shè)置成系統(tǒng)范圍,你必須同時(shí)提供一個(gè) systemPath 元素。注意該范圍是不推薦使用的(你應(yīng)該一直盡量去從公共或定制的 Maven 倉庫中引用依賴)。

http://blog.csdn.net/yhao2014...
https://qiita.com/Dog404/item...
http://blog.csdn.net/wangxiao...
http://www.jianshu.com/p/0e53...

java代碼

繼承HttpServlet類

@WebServlet("/helloworld")
public class HelloWorld extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("text/html");
    PrintWriter out = resp.getWriter();
    out.println("

Hello world

"); } }

如果不使用注解@WebServlet,需要配置web.xml


    HelloWorld
    com.meituan.servlet.HelloWorld



    HelloWorld
    /helloworld

其他兩種servlet實(shí)現(xiàn)方式

實(shí)現(xiàn)javax.servlet.Servlet;

@WebServlet("/helloworld")
public class HelloWorld implements Servlet{
  public void init(ServletConfig config) throws ServletException {

  }

  public ServletConfig getServletConfig() {
      return null;
  }

  public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
      res.setContentType("text/html");
      PrintWriter out = res.getWriter();
      out.println("實(shí)現(xiàn)servlet接口");
  }

  public String getServletInfo() {
      return null;
  }

  public void destroy() {

  }
}

繼承GenericServlet

@WebServlet("/helloworld")
public class HelloWorld extends GenericServlet {
    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        out.println("繼承GenericServlet");
    }
}
servlet亂碼解決方式

原博:http://blog.csdn.net/xiazdong...

常識(shí)

GBK包含GB2312,即如果通過GB2312編碼后可以通過GBK解碼,反之可能不成立;

java.nio.charset.Charset.defaultCharset() 獲得平臺(tái)默認(rèn)字符編碼;

getBytes() 是通過平臺(tái)默認(rèn)字符集進(jìn)行編碼;

中文亂碼出現(xiàn)

在學(xué)習(xí)任何一門技術(shù)時(shí),經(jīng)常會(huì)有初學(xué)者遇到中文亂碼問題,比如MySQL,是因?yàn)樵诎惭b時(shí)沒有設(shè)置;而在Servlet中,也會(huì)遇到中文亂碼問題;
比如:
OutputStream out = response.getOutputStream();
out.write(String );
輸出中文時(shí)可能會(huì)出現(xiàn)亂碼;
比如:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  

        OutputStream out = response.getOutputStream();  
        String data = "博客";  
        out.write(data.getBytes("UTF-8"));  
}  

輸出亂碼的問題是程序用UTF-8編碼,而瀏覽器用GB2312解碼,因此會(huì)出現(xiàn)亂碼;
Servlet亂碼分為request亂碼和response亂碼;

response中文亂碼

在網(wǎng)上很有效的解決方法是添加:
response.setCharacterEncoding("UTF-8");
解決不了,后來又搜到一條解決方法是:
respnse.setHeader("content-type","text/html;charset=UTF-8");
兩句都填上,后來終于解決了這個(gè)問題;
其實(shí)我們應(yīng)該思考一下本質(zhì);

問題1:

我們這里先來說明一下錯(cuò)誤的原因,下圖是顯示亂碼的流程圖:

response.setContentType("text/html;charset=UTF-8"); 目的是為了控制瀏覽器的行為,即控制瀏覽器用UTF-8進(jìn)行解碼;
response.setCharacterEncoding("UTF-8"); 的目的是用于response.getWriter()輸出的字符流的亂碼問題,如果是response.getOutputStream()是不需要此種解決方案的;因?yàn)檫@句話的意思是為了將response對(duì)象中的數(shù)據(jù)以UTF-8解碼后發(fā)向?yàn)g覽器;

解決方案流程圖:

問題2:

問題代碼如下:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
        PrintWriter out = response.getWriter();  
        String data = "博客";  
        out.println(data);    
}  

瀏覽器輸出: ??
原因:"博客"首先被封裝在response對(duì)象中,因?yàn)镮E和WEB服務(wù)器之間不能傳輸文本,然后就通過ISO-8859-1進(jìn)行編碼,但是ISO-8859-1中沒有“博客”的編碼,因此輸出“??”表示沒有編碼;

錯(cuò)誤代碼流程圖:

而解決方案是:response.setCharacterEncoding("GB2312"); 設(shè)置response使用的碼表

解決方案流程圖:

補(bǔ)充:通過標(biāo)簽?zāi)Mresponse頭;

等價(jià)于 response.setContentType("text/html");

request亂碼問題

request請(qǐng)求分為post和get,對(duì)于不同的請(qǐng)求方式有不同的解決亂碼的方案;

1.post請(qǐng)求亂碼

解決方案:

2.get請(qǐng)求亂碼

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/67994.html

相關(guān)文章

  • tomcat

    摘要:服務(wù)器插件在其他的服務(wù)器進(jìn)程內(nèi)部地址空間啟動(dòng)一個(gè)虛擬機(jī),容器組件在此虛擬機(jī)中運(yùn)行。如有客戶端發(fā)出調(diào)用請(qǐng)求,服務(wù)器插件獲得對(duì)此請(qǐng)求的控制并轉(zhuǎn)發(fā)給容器組件使用通訊機(jī)制,即本地調(diào)用接口。 博文參考 http://xtony.blog.51cto.com/3964396/988706/ http://blog.sina.com.cn/s/blog_a0e7e34c01015nes.html h...

    李世贊 評(píng)論0 收藏0
  • tomcat

    摘要:服務(wù)器插件在其他的服務(wù)器進(jìn)程內(nèi)部地址空間啟動(dòng)一個(gè)虛擬機(jī),容器組件在此虛擬機(jī)中運(yùn)行。如有客戶端發(fā)出調(diào)用請(qǐng)求,服務(wù)器插件獲得對(duì)此請(qǐng)求的控制并轉(zhuǎn)發(fā)給容器組件使用通訊機(jī)制,即本地調(diào)用接口。 博文參考 http://xtony.blog.51cto.com/3964396/988706/ http://blog.sina.com.cn/s/blog_a0e7e34c01015nes.html h...

    hightopo 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<