Talking about the relationship between servlet and jsp, talking about servletjsp

Source: Internet
Author: User

Talking about the relationship between servlet and jsp, talking about servletjsp

Servlet is written in java and a java class. The main function is to accept and process client requests, and return the processing results to the client for display. Jsp is a product of the later development of servlet. Before jsp is available, servlet uses the output stream to dynamically generate the entire HTML page. The output content includes each HTML Tag and each HTML page content. HTML files contain a large number of tags, a large number of static texts and formats, and all the presentation logic, including layout, color, and image. These contents must be coupled in java code, which leads to the inefficiency of servlet development, which is annoying. Jsp makes up for the deficiency after it appears, because the jsp file is formed by inserting java code in the standard HTML page. The static part does not require java program control. java Script control is used only when the static part needs to be read from the database and dynamically generated based on the program. Therefore, after the emergence of jsp technology, jsp files are mainly used to dynamically generate HTML files and then return to the client for display. When the current servlet needs to return the entire page as a result, it will not be processed by itself, but will call the jsp file.

The following shows how to develop and deploy a simple servlet program:

1. Create a servlet File to process the request:

 1 package com.servlet.study; 2  3 import java.io.IOException; 4 import java.io.PrintWriter; 5  6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse;10 11 public class HelloWorldServlet extends HttpServlet {12     @Override13     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {14         resp.setContentType("text/html;charset=UTF-8");15         String userName = req.getParameter("username");16         String passWord = req.getParameter("password");17         PrintWriter out = resp.getWriter();18         out.print("

2. Create an HTML file:

1 <! DOCTYPE html> 2 

3. Configure servlet in web. xml:

1 <servlet> 2 <servlet-name> HelloWorldServlet </servlet-name> 3 <servlet-class> com. servlet. study. helloWorldServlet </servlet-class> // implementation class 4 </servlet> 5 <servlet-mapping> // ing 6 <servlet-name> HelloWorldServlet </servlet-name> 7 <url-pattern>/helloworld_servlet </url-pattern> // "/" is required 8 </servlet-mapping>

Note:] The servlet class must inherit the HttpServlet class, And the doGet and doPost methods must be rewritten, And the out object must be created.

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.