On the Relationship between servlet and jsp

  • 2020-06-15 08:25:55
  • OfStack

servlet is written in the java language and is an java class. The main function is used to receive, process the client request, and return the processing result to the client display. Jsp is the product of the late development of servlet. Before jsp, servlet USES an output stream to dynamically generate the entire HTML page, with output for each HTML tag and each HTML page appearance. The HTML file contains a lot of tags and a lot of static text and formatting, and all the presentation logic, including layout, colors, and images. All of this has to be coupled to java code, which makes servlet annoyingly inefficient. jsp made up the difference, because jsp files are formed by inserting java code into standard HTML pages. The static parts are not controlled by the java program, only those that need to read from the database and dynamically generate information from the program are controlled by the java script. Therefore, after the emergence of jsp technology, jsp files are mainly used to dynamically generate HTML files, and then returned to the client display. servlet now calls the jsp file instead of handling the entire page itself when it needs to be returned as a result.

Develop and deploy a simple servlet program to demonstrate:

1. Create servlet files for processing requests:


package com.servlet.study;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorldServlet extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    super.doGet(req, resp);
  }
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("text/html;charset=UTF-8");
    req.setCharacterEncoding("UTF-8");
    String userName = req.getParameter("username");
    String passWord = req.getParameter("password");
    PrintWriter out = resp.getWriter();
    out.print("<html>");
    out.print("<head>");
    out.print("<title>Helloworld</title>");
    out.print("</head>");
    out.print("<body>");
    out.print("<hr>");
    out.println("The username is "+userName);
    out.println("The password is "+passWord);
    out.print("</body>");
    out.print("</html>");
  }
}

2. Create HTML file:


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> User login page </title>
</head>
<body>
<h1 align="center"> Login system </h1><hr>
<form action="helloworld_servlet" method = "post">// The form of action It actually indicates that servlet the url                
  <table>
    <tr>
      <td> The user name </td>
      <td><input type="text" name="username"></td>
    </tr>
    <tr>
      <td> password </td>
      <td><input type="password" name="password"></td>
    </tr>
    <tr>
      <td><input type="reset" value=" refill "></td>
      <td><input type="submit" value=" submit "></td>
    </tr>
  </table>
</form>
</body>
</html>

3. Configure servlet in ES38en. xml:


<servlet>
    <servlet-name>HelloWorldServlet</servlet-name>
    <servlet-class>com.servlet.study.HelloWorldServlet</servlet-class>// The implementation class 4</servlet>
  <servlet-mapping>// mapping 
    <servlet-name>HelloWorldServlet</servlet-name>
    <url-pattern>/helloworld_servlet</url-pattern>// " / "Is a must 
  </servlet-mapping>

Note: the]servlet class must inherit from the HttpServlet class and override the doGet and doPost methods and create out objects. The doGet method is the method in the HttpServlet class that handles get requests, and doPost handles post requests. Declare method in the form and write the corresponding method in the servlet class, in this case specifically the post request.


Related articles: