目錄
學習目標
- WEB服務器tomcat
- servlet
WEB服務器
- Web服務器是指能夠爲發出請求的瀏覽器提供文檔的程序。服務器是一種被動程序,只有瀏覽器發出請求的時候纔會響應。應用層使用的是HTTP協議。
- 常用web服務器
- apache
- nginx
- iis
- tomcat
廣義的servlet
- Servlet(Servlet Applet)是Java Servlet的簡稱,用Java編寫的服務器端程序。用來接收和響應web客戶端的請求,主要用於動態生成web頁面內容。
- Servlet沒有main方法,不能獨立運行,因此必須把它部署到Servlet容器中,由容器來實例化並調用Servlet。
tomcat簡介
Tomcat是Apache軟件基金會(Apache Software Foundation)的Jakarta項目中的一個核心項目,使用java語言編寫的,完全運行在jvm上,實現了Servlet規範和jsp規範。2.5/2.1-6.X
Tomcat的核心分爲3個部分:
(1)Web容器—處理靜態頁面;
(2)catalina—一個servlet容器-----處理servlet
(3)還有就是JSP容器,它就是把jsp頁面翻譯成一般的servlet。
apache-tomcat目錄結構
- bin:存放二進制文件,啓動腳本等
- work:工作區。文件編譯存放區
- lib:web應用所依賴的庫
- logs:記錄程序啓動及運行時的日誌
- temp:存放臨時文件
- webapps:web應用默認存放的目錄
- conf:配置文件目錄
練習:servlet完成查詢所有用戶並顯示
運行結果截圖
工程截圖
User.java代碼
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代碼
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代碼
<%@ 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代碼
<%@ 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="登陸"/> <input type="reset" value="重置" /></td>
</form>
</body>
</html>