国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

servlet系列-Tomcat、webapp、servlet初步

tuantuan / 1437人閱讀

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

關于服務器 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的知識

compile (編譯范圍)

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

provided (已提供范圍)

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

runtime (運行時范圍)

runtime 依賴在運行和測試系統的時候需要,但在編譯的時候不需要。比如,你可能在編譯的時候只需要JDBC API JAR,而只有在運行的時候才需要JDBC
驅動實現。

test (測試范圍)

test范圍依賴 在一般的編譯和運行時都不需要,它們只有在測試編譯和測試運行階段可用。

system (系統范圍)

system范圍依賴與provided 類似,但是你必須顯式的提供一個對于本地系統中JAR 文件的路徑。這么做是為了允許基于本地對象編譯,而這些對象是系統類庫的一部分。這樣的構件應該是一直可用的,Maven 也不會在倉庫中去尋找它。如果你將一個依賴范圍設置成系統范圍,你必須同時提供一個 systemPath 元素。注意該范圍是不推薦使用的(你應該一直盡量去從公共或定制的 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實現方式

實現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("實現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...

常識

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

java.nio.charset.Charset.defaultCharset() 獲得平臺默認字符編碼;

getBytes() 是通過平臺默認字符集進行編碼;

中文亂碼出現

在學習任何一門技術時,經常會有初學者遇到中文亂碼問題,比如MySQL,是因為在安裝時沒有設置;而在Servlet中,也會遇到中文亂碼問題;
比如:
OutputStream out = response.getOutputStream();
out.write(String );
輸出中文時可能會出現亂碼;
比如:

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解碼,因此會出現亂碼;
Servlet亂碼分為request亂碼和response亂碼;

response中文亂碼

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

問題1:

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

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

解決方案流程圖:

問題2:

問題代碼如下:

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

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

錯誤代碼流程圖:

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

解決方案流程圖:

補充:通過標簽模擬response頭;

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

request亂碼問題

request請求分為post和get,對于不同的請求方式有不同的解決亂碼的方案;

1.post請求亂碼

解決方案:

2.get請求亂碼

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/67994.html

相關文章

  • tomcat

    摘要:服務器插件在其他的服務器進程內部地址空間啟動一個虛擬機,容器組件在此虛擬機中運行。如有客戶端發出調用請求,服務器插件獲得對此請求的控制并轉發給容器組件使用通訊機制,即本地調用接口。 博文參考 http://xtony.blog.51cto.com/3964396/988706/ http://blog.sina.com.cn/s/blog_a0e7e34c01015nes.html h...

    李世贊 評論0 收藏0
  • tomcat

    摘要:服務器插件在其他的服務器進程內部地址空間啟動一個虛擬機,容器組件在此虛擬機中運行。如有客戶端發出調用請求,服務器插件獲得對此請求的控制并轉發給容器組件使用通訊機制,即本地調用接口。 博文參考 http://xtony.blog.51cto.com/3964396/988706/ http://blog.sina.com.cn/s/blog_a0e7e34c01015nes.html h...

    hightopo 評論0 收藏0

發表評論

0條評論

最新活動
閱讀需要支付1元查看
<