. NET C # to Java is not that hard, servlet article

Source: Internet
Author: User
Tags locale java web

Objective

. NET C # to Java is not so difficult, are oriented to the language, and the syntax is similar, the first comparison of the development environment, and then to the servlet, and then to MVC, are the same, but Java is more than the configuration, as long as a good one, the back is copy, so easy , and with MyEclipse can save some preparation

The difference between Java and. NET is not in their own, the biggest difference is the power behind them, I believe everyone can feel it, I don't say much.

Directory

One,. NET C # to Java is not that difficult, development environment chapter

Second,. NET C # to Java is not that difficult, servlet chapter

Third,. NET C # to Java is not that difficult, MVC article

Iv.. NET C # to Java is not that difficult, MVC framework

V.. NET C # to Java is not that difficult, DB framework

Vi.. NET C # to Java it's not that hard, NoSQL framework article

Vii.. NET C # to Java is not that difficult, micro-service Chapter

Viii.. NET C # to Java is not that difficult, big data article

. NET C # to Java is not that hard, servlet article

1. Servlet and Ashx ( generic handler )

Java Web and ASP are similar, the core of the Java Web is servlet,jsp is actually the servlet, and the servlet process can be relatively clear to see how the entire front and back of the work, similar. NET ASHX General Handler, Java is the main advocate of MVC, with the development of web technology, servlet is mainly used as controler, JSP is used as view, which is embodied in many MVC frameworks. Thanks to the active open source community, popular Java The MVC framework is more mature.

2.Servlet and JSP

JSP is eventually transformed into a Servlet,.java is a servlet

When a JSP file is requested for the first time, Tomcat first converts the JSP file into a Java source file. During the conversion process, if a JSP file is found to have syntax errors, the conversion process is interrupted and an error message is output to the server and the client. If the conversion succeeds, Tomcat compiles the Java source file into the appropriate. class file with Javac and loads the. class file into memory.

3. The life cycle of the servlet

1). The servlet life cycle is divided into three phases:

1, initialization phase: Call Init () method

2, responding to customer request phase: Call Service () method

The Service () method internally determines the type of request (Get/post) and automatically calls Dopost/doget

3, termination phase: Call Destroy () method

2). Single-threaded servlet:

Singleton: A servlet is instantiated only when the user first requests it, and is a singleton that is destroyed when the server restarts or shuts down.

Multithreading: servlet container (Tomcat ...) when a request arrives Each thread executes a single Servlet instance's service () method by giving the requestor and executing the service method through the threads available in the thread pool

4. Java Filter and Httpmodule,actionfilter1). Java Filter

Filter, also known as filters, is the most practical technology in Servlet technology, Web developers through the filter technology, Web server management of all Web resources: such as JSP, Servlet, static picture files or static HTML files, etc. to intercept, So that some special functions can be realized. For example, the implementation of URL-level access control, filtering sensitive words, compressed response information and other advanced features.

It is mainly used to preprocess user requests, or to post-process httpservletresponse. Use the full process of filter: Filter to preprocess the user request, then send the request to the servlet for processing and generate a response, and finally filter to post-process the server response.

Java Filter Filter

2). HttpModule and Actionfilter

HttpModule is the filter of the ASP. NET WebForm

Talking about HttpModule

Actionfilter is the filter for ASP.

Overview of the four basic filter

5. JSP contains 9 large objects, which can correspond to ASP. 1. Request Object

The client's request information is encapsulated in the requests object to understand the customer's needs and respond. It is an instance of the HttpServletRequest class.

2). Response and Out objects

The Response object contains information about the response to a client request, which is an instance of the HttpServletResponse class.
An Out object is an instance of the JspWriter class and is an object commonly used to output content to the client
Response and Out

3). Session Object

The session object refers to a client-to-server conversation, from the client to a server
WebApplication starts until the client disconnects from the server. It's httpsession.
The instance of the class.

4). Page and PageContext objects

The Page object is pointing to the current JSP page itself, a bit like the this pointer in the class, which is an instance of the Java.lang.Object class
The PageContext object provides access to all objects and namespaces within the JSP page, i.e.
Says he can access the session where this page is located, or you can take the application
A property value that is equivalent to the synthesizer of all the functions in the page, and the name of the class is also called

Page and PageContext

5). Application Object

The Application object implements the sharing of data among users and can store global variables. It starts at the server
Until the server shuts down, during which time this object will persist;
The same property of this object can be manipulated in the connection between a post-connection or a different user;
Local operations on this object's properties will affect access to it by other users. Server Start-up and
Close determines the life of the Application object. It is an instance of the ServletContext class.

6). Exception Object

The exception object is an exception, and when an exception occurs during a page run
The object of the birth. If a JSP page is to be applied to this object, the Iserrorpage must be set to True,
Otherwise, it cannot be compiled. He's actually a Java.lang.Throwable object.

7). config Object

The Config object is used by the JSP engine to pass information to it when a servlet is initialized, including the parameters to be used when the servlet initializes (through property names and property values) and information about the server (by passing a ServletContext object)

6. Servlet Sample Code

1). Hello World Sample Code

//Import the required Java librariesImportJava.io.*;Importjavax.servlet.*;Importjavax.servlet.http.*;//extended HttpServlet class Public classHelloWorldextendsHttpServlet {PrivateString message;  Public voidInit ()throwsservletexception {//perform the required initializationmessage = "Hello World"; }   Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {//Setting the response content typeResponse.setcontenttype ("text/html"); //The actual logic is herePrintWriter out =Response.getwriter (); Out.println ("); }     Public voiddestroy () {//I don't do anything.  }}

2). Web-inf/web.xml Compounding

<web-app>          <servlet>        <servlet-name>HelloWorld</servlet-name>        < servlet-class>helloworld</servlet-class>    </servlet>    <servlet-mapping>        < servlet-name>helloworld</servlet-name>        <url-pattern>/HelloWorld</url-pattern>    </servlet-mapping></web-app>  

3). Then run tomcat to access your servlet

7.ServletInternationalization

Before we get started, let's look at three important terms:

    • internationalization (i18n): This means that a website offers different versions of the content translated into the language or nationality of the visitor.
    • localization (l10n): This means adding resources to the site to accommodate specific geographic or cultural areas, such as a website translated into Hindi (Hindi).
    • locale: This is a special cultural or geographic area. It usually refers to a language symbol followed by an underscore and a national symbol. For example, "en_US" indicates an English locale for US.

There are a few things to note when building a global website. This tutorial will not cover the full details of these considerations, but it will show you how to make a Web page appear in different languages through a good example.

The Servlet can pick up the appropriate version of the site based on the requestor's locale and provide the appropriate site version based on the local language, culture, and needs. The following is a method that returns a Locale object in the Request object.

Java.  Util.  Locale request.  GetLocale()       
Detect Regional Settings

The following is a list of important locale methods that you can use to detect the location, language, and locale of the requestor. All of the following methods show the country name and language name set in the requestor's browser.

Serial Number Method & Description
1 String Getcountry ()
This method returns the country/region code for the locale in 2 uppercase letters in ISO 3166 format.
2 String Getdisplaycountry ()
The method returns the name of the country that is appropriate for the locale that is displayed to the user.
3 String GetLanguage ()
The method returns the language code for the locale in the lowercase letter ISO 639 format.
4 String Getdisplaylanguage ()
The method returns the name of the language that is appropriate for the locale that is displayed to the user.
5 String Getiso3country ()
The method returns the three-letter abbreviation of the country that the locale is set to.
6 String Getiso3language ()
The method returns a three-letter abbreviation for the language of the locale.

Deep understanding of Java internationalization

Servlet reference

Not finished, to be continued, welcome to comment on making bricks

. NET C # to Java is not that hard, servlet article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.