Servlet and JSP Overview

Source: Internet
Author: User

When it comes to Java Web Development, you have to mention Servlets and JSPs. The two are Java Web development technologies. Although there are so many web frameworks in the company, it is very rare to use these two technologies to directly develop projects, but understanding the two technologies will give you a good understanding of the basics of the web, and second, to better understand the point than others.

First, the Servlet

1. What is a servlet?

A servlet is a Java program that runs on a Web server. is a middle tier that is responsible for connecting databases or applications from Web browsers or other HTTP requests and HTTP servers.

2. What is the role of the servlet?

A. Read the display data sent by the user and the implicit request data sent by the browser.

The display data is generally the form data entered on the page.

Implicit data is HTTP information, such as cookies, which the browser can identify with other media types or compression modes.

B. Generate the results.

This is where business logic is handled, such as accessing databases or other complex operations. Results and HTML content that need to be returned directly to the browser are also generated.

C. Send display data and implicit HTTP response data to the customer.

Displays the data as a result that the user can see.

HTTP response data, such as cookie settings and cache parameters. This information is read and processed by the client browser.

3. servlet Example

This example is just for the reader to have an understanding of the servlet, no other configuration, it can not be directly run, there will be a detailed introduction.

 Packagedemo0103;Importjava.io.IOException;ImportJava.io.PrintWriter;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classServletoverviewextendshttpservlet{ Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, ioexception{String contentType= "Text/html";        Response.setcontenttype (ContentType); PrintWriter writer=Response.getwriter (); String DocType= "<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" >\n "; String HTML= "; Writer.println (DocType+html); }}
View Code

Show running results:

Second, JSP

1. What is a JSP?

JSP (full name Java Server pages) is a technical standard created by Sun Microsystems and many companies involved in creating a Web page that enables software developers to respond to client requests and dynamically generate HTML, XML, or other format documents.

2. What is the role of JSP?

The JSP can accomplish the functions that the servlet can accomplish.

In a nutshell, a servlet can be thought of as a Java program that contains HTML. Consider a JSP as a page that contains HTML for Java code.

3. JSP Example

<!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML><Head><title>Hello World</title></Head><Body>    <H1>Hello World</H1></Body></HTML>
View Code

Show Results:

The servlet can do what the JSP can do, and the same JSP can do the functions that the servlet can do. How to choose?

For convenience, ease-of-use and maintainability, the second combination is the best way to handle it.

Servlets are the best choice for the task of business logic.

JSP is the best choice for the tasks that are displayed.

The MVC pattern that follows is a good illustration of the main application.

The above is only their own understanding, if there are irregularities, also hope to point out.

Servlet and JSP Overview

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.