.NET C # compared to Java – Servlet

Directory

1. Servlet and Ashx (general processing program)

2. Servlet and JSP

3. Servlet life cycle< /h3>

4. Java Filter and HttpModule, ActionFilter

5. JSTL, EL and Razor expression

6. JSP built-in 9 large objects, which can be used with ASP.NET Corresponding

7. Servlet sample code

8. Servlet Internationalization

1. Servlet and Ashx (general processing program)

Java Web and ASP.NET are also similar. The core of java web is Servlet, and Jsp is actually Servlet. The Servlet process can clearly see how the entire front-end and back-end work, similar to the ashx general processing program of .NET, Java is the main advocate of MVC, with the development of web technology, Servlet is mainly used as a Controler, and Jsp is used as View use, which is reflected in many MVC frameworks. Thanks to the active open source community, the popular Java MVC frameworks are more mature.

share pictures

2. Servlet and JSP

jsp is finally transformed into Servlet, .java is a Servlet

When a JSP file is requested for the first time, Tomcat will first convert the JSP file into a Java source file. During the conversion process, if a grammatical error is found in the JSP file, the conversion process will be interrupted and error messages will be output to the server and client. If the conversion is successful, Tomcat uses javac to compile the Java source file into the corresponding .class file and load the .class file into the memory.

3. Servlet life cycle

1). Servlet life cycle is divided into three phases:

  1, initialization phase : Invoke the init() method

  2, in response to the customer request stage: call the service() method

    The service() method internally judges the type of request (get/post), Automatically call doPost/doGet

  3, termination stage: call destroy() method

2). Servlet singleton multithreading:

   singleton: Servlet It is instantiated only when the user requests it for the first time, and it is singleton, and it will be destroyed when the server is restarted or shut down.

   Multithreading: When the request arrives, the Servlet container (Tomcat…) gives the requester through the threads available in the thread pool and executes the Service method. Each thread executes the service( of a single Servlet instance). ) Method

Share pictures

4. Java Filter and HttpModule, ActionFilter

1).Java Filter

Filter is also called a filter. It is the most practical technology in Servlet technology. Web developers use Filter technology to address web servers. All web resources managed: such as Jsp, Servlet, static image files or static html files are intercepted to achieve some special functions. For example, realize some advanced functions such as URL-level permission access control, filtering sensitive words, and compressing response information.

It is mainly used to pre-process user requests, and it can also post-process HttpServletResponse. The complete process of using Filter: Filter preprocesses the user request, then passes the request to the Servlet for processing and generates a response, and finally Filter post-processes the server response.

Java Filter

2).HttpModule and ActionFilter

HttpModule is the Filter of asp.net WebForm

Talking about HttpModule< /p>

ActionFilter is the Filter of asp.net MVC

Overview of the four basic Filters

5. JSTL, EL and Razor expressions< /h2>

Make the code labelled and expressive, so that front-end personnel can maintain it, and realize the separation of front-end and back-end

1). JSTL tag

JSP standard tag library ( JSTL) is a collection of JSP tags, which encapsulate the common core functions of JSP applications.

JSTL supports general and structured tasks, such as iteration, conditional judgment, XML document manipulation, international tags, and SQL tags. In addition to these, it also provides a framework to use custom tags integrated with JSTL.

2).EL expression

EL (Expression Language) Purpose: To make JSP writing easier.

It provides a way to simplify expressions in JSP. It is a simple language, based on available namespaces (PageContext attributes), nested attributes, accessors to collections, operators (arithmetic, relational, and logical), and can be mapped to static methods in Java classes. Extension functions and a set of implicit objects.

3).Razor

Here is the comparison of Razor expressions

6. JSP has 9 built-in objects, which can be compared with ASP .NET Correspondence

1).request object

The client’s request information is encapsulated in the request object, through which the customer’s needs can be understood, and then the response can be made. It is an instance of the HttpServletRequest class.

2).response and out objects

The response object contains information about responding to customer requests. It is an instance of the HttpServletResponse class.
The out object is an instance of the JspWriter class, which is a commonly used object for outputting content to the client
response and out

3).session object

session object refers to A session between the client and the server starts from a
WebApplication that the client connects to the server until the client disconnects from the server. It is an instance of the HttpSession
class.

4).page and pageContext objects

The page object points to the current JSP page itself, a bit like the this pointer in the class. It is An instance of the java.lang.Object class
The pageContext object provides access to all the objects and namespaces in the JSP page, that is,
that it can access the SESSION where the page is located, or it can take the page where it is. An attribute value of the application
, which is equivalent to the master of all functions in the page, and its class name is also called

page and pageContext

5).application Object

The application object realizes the sharing of data between users and can store global variables. It starts from the start of the server
, until the server is shut down, during this period, this object will always exist; so in the user’s previous
connection or connection between different users, this object can be the same Attribute operation; any operation on this object attribute will affect other users’ access to it. The startup and shutdown of the server determines the life of the application object. It is an instance of the ServletContext class.

6).exception object

The exception object is an exception object. When an exception occurs in a page during operation, this object is produced. If a JSP page wants to use this object, it must set isErrorPage to true, otherwise it will not compile. He is actually the object of Java.lang.Throwable

7).config object

The config object is used by the JSP engine to pass information to a servlet when it is initialized. This information Including the parameters used during Servlet initialization (constituted by attribute names and attribute values) and server related information (by passing a ServletContext object)

7. Servlet sample code

1).Hello World sample code

// Import the necessary java libraries

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend the HttpServlet class
public class HelloWorld extends HttpServlet {

private String message;

public void init() throws ServletException
{
// Perform necessary initialization
message = "Hello World";
}

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Set response content type
response.setContentType("text/html");

// The actual logic is here
PrintWriter out = response.getWriter();
out.println(
"

" + message + "

");
}

public void destroy()
{
// do nothing
}
}

2).WEB-INF/ web.xml configuration




HelloWorld
class>HelloWorldclass>



HelloWorld
/HelloWorld

3). Then run tomcat and visit your Servlet

share picture

8. Servlet< span class="color_h1">Internationalization

Before we start, let’s take a look at three important terms:

  • International Translated (i18n): This means that a website provides different versions of content translated into the language or nationality of the visitor.
  • Localization (l10n): This means adding resources to the website to adapt it to a specific geographic or cultural area, for example, the website is translated into Hindi.
  • Locale: This is a special cultural or geographic area. It usually refers to a language symbol followed by an underscore and a country symbol. For example, “en_US” means the English locale for US.

There are some considerations when building a global website. This tutorial will not explain the complete details of these precautions, but it will show you how to make web pages appear in different languages ​​through differentiated positioning (ie, regional settings) through a good example.

Servlet can pick up the website of the corresponding version according to the requester’s regional settings, and provide the corresponding website version according to the local language, culture and needs. The following is the method in the request object that returns the Locale object.

java.util.Locale request.getLocale() 

Detection area setting

Important locale setting methods are listed below. You can use them to detect the requester’s geographic location, language, and locale. All the methods below show the country name and language name set in the requester’s browser.

< th>Method & Description

Serial number
1 String getCountry()
This method returns the locale in ISO 3166 format in 2 capital letters The country code of.
2 String getDisplayCountry()
This method returns the name of the country suitable for the locale displayed to the user.
3 String getLanguage()
This method returns the language code of the locale in ISO 639 format in lowercase letters.
4 String getDisplayLanguage()
This method returns the name of the language suitable for the locale that is displayed to the user.
5 String getISO3Country()
This method returns the three-letter abbreviation of the country in the locale.
6 String getISO3Language() This method returns the three-letter abbreviation of the language of the locale.

// Import the necessary java libraries

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend the HttpServlet class
public class HelloWorld extends HttpServlet {

private String message;

public void init() throws ServletException
{
// Perform necessary initialization
message = "Hello World";
}

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Set response content type
response.setContentType("text/html");

// The actual logic is here
PrintWriter out = response.getWriter();
out.println(
"

" + message + "

");
}

public void destroy()
{
// do nothing
}
}




HelloWorld
class>HelloWorldclass>



HelloWorld
/HelloWorld

Serial number Method & Description
1 String getCountry()
This method is written in 2 capital letters The ISO 3166 format returns the country/region code of the locale.
2 String getDisplayCountry()
This method returns the name of the country suitable for the locale displayed to the user.
3 String getLanguage()
This method returns the language code of the locale in ISO 639 format in lowercase letters.
4 String getDisplayLanguage()
This method returns the name of the language suitable for the locale displayed to the user.
5 String getISO3Country()
This method returns the three-letter abbreviation of the country in the locale.
6 String getISO3Language() This method returns the three-letter abbreviation of the language of the locale.

Leave a Comment

Your email address will not be published.