JSP Java Servlet
Java Servlet 即“服务器连接器”,主要用于创建Web应用,可在交互式地浏览和生成数据(动态Web内容)。当用户请求一个JSP页面的Web服务器,Servlet会自动生成一个对应的Java对文件。通过编译该Java文件用编译得到的字节码文件在服务器端创建一个Servlet对象。但在项目的实际需求之中,Web服应用需要Servlet对象有特定的功能,需要编写和创建Servlet对象和类就是本章重点介绍的一部分。
部署 Servlet
相关的接口和类
| ID |
DA |
| protected void doGet(HttpServletRequest request, HttpServletResponse response) |
请求指定资源 |
| protected void doPost(HttpServletRequest request, HttpServletResponse response) |
向指定的资源提交要被处理的数据 |
创建一个Servlet类
Eclipse Web
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| package demofile;
import java.io.IOException; import java.io.PrintWriter;
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
@WebServlet("/NewFile") public class NewFile extends HttpServlet { private static final long serialVersionUID = 1L;
public NewFile() { super(); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("Served at: ").append(request.getContextPath()); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.println("<br>"); out.println("Hello,world!"); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }
}
|
需要注意的事情是,Servlet可使用Eclipse Web 内自带的Servlet类型文件进行直接的创建,Servlet类必须要有一个包名,不然的话不是一个完整的Servlet类文件。在生成Servlet的同时,Eclipse会自动创建和部署有关的xml文件信息,通常该文件名称为web.xml。
@WebServlet
@WebServlet用于将一个类声明为Servlet对象,该注解将会在开始运行服务器时将部署时被Web容器根据Servlet对象进行处理.@WebServlet (name = "newfile", urlPatterns = {"newfile"}) 有兴趣的读者可自行进行了解或学习。
Servlet 生命周期
一个Servlet生命周期主要分为三个过程,分别为:初始化对象(int)、方法响应请求(service)、对象消亡(destroy),也就是说当Servlet对象第一次被请求加载的时候会创建一个Servlet对象,主要调用int()方法进行初始化。Service()方法响应请求,创建的Servlet对象之后调用service()方法响应客户端进行请求。当服务器进行关闭时,Servlet对象会调用destroy()方法来使得对象销毁。
三个周期输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| package demofile;
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
@WebServlet("/NewFile") public class NewFile extends HttpServlet { private static final long serialVersionUID = 1L;
public NewFile() { super(); } @Override public void init() throws ServletException { System.out.println("初始化时输出"); }
@Override protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException { System.out.println("开启服务时输出"); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } @Override public void destroys() { System.out.println("结束时输出"); } }
|
JSP 调用Servlet
NewFile.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="loginServlet" method="post"> <%-- User = user \ Pass = pass submit up \ reset set--%> <table> <tr> <td>User:</td> <td><input type="text" name="user"/></td> </tr> <tr> <td>pass:</td> <td><input type="text" name="pass"/></td> </tr> <tr> <td><input type="submit" value="up"/></td> <td><input type="reset" value="set"></td> </tr> </table> </form> </body> </html>
|
loginServlet.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| package demofile;
import java.io.IOException; import java.io.PrintWriter;
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
@WebServlet("/loginServlet") public class loginServlet extends HttpServlet { private static final long serialVersionUID = 1L;
public loginServlet() { super(); }
protected void service (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter();
String name = request.getParameter("user"); String password = request.getParameter("pass"); out.println("<html><body>"); if (name == null || name.length() ==0) { out.println("user null"); } else if(password == null || name.length() == 0) { out.println("pass null"); } else if(name.length() > 0 && password.length() >0) { if(name.equals("sun") && password.equals("kun")) { out.println("ok"); } else { out.println("no"); } } out.println("</body></html>"); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("Served at: ").append(request.getContextPath()); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }
}
|
| ID |
DA |
| getParameter |
获取input输出数据 |
Servlet MVC
MVC即“Model、View、Controller”的缩写,分别代表Web应用程序的三种职责,即:
| ID |
DA |
FA |
| 模型 |
用于存储数据以及处理用户请求的业务能力 |
一个或多个JavaBean对象、用于存储数据(实体模型)和处理业务逻辑 |
| 页面 |
向控制器提交数据,显示模型中的数据 |
一个或多个Jsp页面,向Servlet提交数据和为模型提供数据显示(JSP主要使用HTML来标记JavaBean来显示数据) |
| Servlet |
根据页面提出的请求来判断将请求和数据交给那个模型中处理,处理后的那个页面更新显示 |
一个或多个Servlet对象,根据页面提交的请求进行控制。(即将请求转发给处理业务逻辑的JavaBean,并将处理结果存放在JavaBean中,市输出给视图显示) |
JSP、Servlet、JavaBean实现MVC
通过使用JSP、Servlet、JavaBean来实现i个简单的MVC用户登录验证程序,其中包括MVC模型、页面、Servlet:
| ID |
DA |
FA |
| User |
用于创建模型,存储用户信息 |
JAVA |
| UserCheck |
判断用户名和密码是否正确 |
|
| LoginCheck |
用于完成请求控制 |
|
|
|
|
| loginCheak.jsp |
用于编写登入界面 |
JSP |
| loginOk.jsp |
登录成功页面 |
|
User.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package demofile; public class User { private String name; private String password; public String getName() { return name; } public void setName(String name){ this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
|
UserCheck (Service)
1 2 3 4 5 6 7 8 9 10 11 12 13
| package service; import demofile.User; public class UserCheck { public boolean validate(User user) { if(user!=null&&user.getName().equals("Sun")) { if(user.getPassword().equals("kun"))return true; else return false; } return false; } }
|
LoginCheck (Servlet)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| package servlet; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import service.UserCheck; import demofile.User;
@WebServlet(name="LoginCheckServlet",urlPatterns= {"/LoginCheckServlet"}) public class LoginCheck extends HttpServlet { private static final long serialVersionUID = 1L;
public LoginCheck() { super(); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String nameuser = request.getParameter("nameuser"); String password = request.getParameter("password"); User user=new User(); user.setName(nameuser); user.setPassword(password); UserCheck uc=new UserCheck(); if(uc.validate(user)) { request.setAttribute("user", user); RequestDispatcher dis =request.getRequestDispatcher("loginOk.jsp"); dis.forward(request, response); }else { response.sendRedirect("loginCheck.jsp"); } } }
|
loginCheak (Jsp)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>loginCheck.jsp</title> </head> <body> <form action="LoginCheckServlet" methods="get"> <table> <tr> <td>User:</td> <td><input type="text" name="nameuser"/></td> </tr> <tr> <td>Pass:</td> <td><input type="password" name="password"></td> </tr> <tr> <td><input type="submit" value="Up"/></td> <td><input type="reset" value="Set"/></td> </tr> </table> </form> </body> </html>
|
loginOK (Jsp)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="demofile.User" %> <!DOCTYPE html"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>loginSuccess.jsp</title> </head> <body> <jsp:useBean id="user" type="demofile.User" scope="request"/> User:<jsp:getProperty property="name" name="user"/> success! </body> </html>
|
⬅️ Go back