Test Development Series --tomcat & servlet

learning target

  • WEB server tomcat
  • servlet

WEB server

  • Web server is a program to provide documentation for the browser making the request. Server is a passive program with only the browser making the request to respond. Application layer protocol is HTTP.
  • Common web server
    1. apache
    2. nginx
    3. iis
    4. tomcat

Generalized servlet

  • Servlet (Servlet Applet) is a Java Servlet short, written in Java Server-side program. Receive and respond to requests for web clients, mainly for dynamically generating web page content.
  • No main Servlet methods, it can not operate independently, so it must be deployed to the Servlet Container, instantiated and invoked by the Servlet container.

tomcat Profile

Tomcat is a core project of the Apache Software Foundation (Apache Software Foundation) in Jakarta project, using java language, runs entirely on jvm, implements the Servlet specification and jsp specifications. 2.5 / 2.1-6.X

Tomcat core divided into three parts:
(1) Web container - handling static pages;
(2) catalina- a servlet container ----- process servlet
(3) There is a JSP container, which is translated into a jsp page general servlet.

apache-tomcat directory structure

  • bin: to store binary files, startup scripts, etc.
  • work: the work area. Files compiled storage areas
  • lib: web applications depend library
  • logs: boot log logger and run-time
  • temp: temporary files
  • webapps: web application stored in the default directory
  • conf: configuration files directory

Exercise: servlet complete the query and display all users

Screenshot operating results

Screenshot operating results

Project Screenshot

Project Screenshot

User.java Code

package com.one.pojo;

import lombok.Data;

@Data
public class User {
  private int userId;
  private String userName;
  private String userPwd;
  public User(int userId, String userName, String userPwd){
    super();
    this.userId = userId;
    this.userName = userName;
    this.userPwd = userPwd;
  }
}

UserServlet.java Code

package com.one.servlet;

import com.one.pojo.User;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
    }

  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String action = request.getParameter("action");
    if("login".equals(action)){
      login(request, response);
    }else if("findAll".equals(action)){
      findAll(request, response);
    }
  }

  private void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    List<User> list = new ArrayList<>();
    list.add(new User(1,"zs","666"));
    list.add(new User(2,"lisi","888"));
    //将list放入request值域中
    request.setAttribute("list", list);
    request.getRequestDispatcher("index.jsp").forward(request, response);
  }
  
  private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // 接受请求并响应
    // 1.获取请求参数并封装为对象
    String username = request.getParameter("username");
    String userpwd = request.getParameter("userpwd");
    // 2.数据验证 非空 正则表达式
    // 3.登录业务处理,得到登陆业务结果
    boolean b = false;
    if("zs".equals(username)&&"666".equals(userpwd)){
      b = true;
    }
    // 4.根据结果进行跳转
    if(b){
      //服务器内部转发,同一请求,地址栏不会变化
      request.getRequestDispatcher("index.jsp").forward(request, response);
    }else{
      response.sendRedirect("login.jsp?msg=error");
    }
  }

  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
  }
}

index.jsp Code

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
  <!-- 欢迎你,<%=request.getParameter("username") %> -->
  欢迎你,${param.username}
  循环${requestScope.list}集合,取出每一个user
</body>
</html>

login.jsp Code

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
  ${param.msg}
  <form action="userServlet" method="post">
    <input name="username" /><br/>
    <input name="action" value="findAll" type="hidden"/><br/>
    <input name="userpwd" type="password"/><br/>
    <input type="submit" value="登陆"/>&nbsp; <input type="reset"  value="重置" /></td>
  </form>
</body>
</html>
Published 14 original articles · won praise 1 · views 855

Guess you like

Origin blog.csdn.net/anniewhite/article/details/104078022