一、何为Servlet
Servlet就是一个接口,定义了Java类被浏览器访问到(tomcat识别)的规则。
将来我们自定义一个类,实现Servlet接口,复写方法。
public class MyServlet implements Servlet
二、如何配置Servlet
1.在web.xml中配置
<!--配置Servlet -->
<servlet>
<servlet-name>SelUserServlet</servlet-name>
<servlet-class>com.beizhen.servlet.SelUserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SelUserServlet</servlet-name>
<url-pattern>/seluser</url-pattern>
</servlet-mapping>
2.通过注解配置
@WebServlet("/seluser")
三、执行原理
1.当服务器接受到客户端浏览器的请求后,会解析请求URL路径,获取访问的Servlet的资源路径
2. 查找web.xml文件,是否有对应的标签体内容。
3. 如果有,则在找到对应的全类名
4. tomcat会将字节码文件加载进内存,并且创建其对象
5. 调用其方法
四、Servlet生命周期
1.Servlet 通过调用 init () 方法进行初始化。init 方法只调用一次。
2.Servlet 调用 service() 方法来处理客户端的请求。
3.Servlet 通过调用 destroy() 方法终止(结束)。destroy() 方法只会被调用一次
4.最后,Servlet 是由 JVM 的垃圾回收器进行垃圾回收的。
五、使用servlet实现学生信息后台管理
学生信息后台管理效果图
项目分包图:
查询所有学生 SelStuServlet
package com.beizhen.servlet;
import com.beizhen.bean.Student;
import com.beizhen.bean.User;
import com.beizhen.dao.StudentDao;
import com.beizhen.dao.UserDao;
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 java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/selstu")
public class SelStuServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置请求和响应编码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//获得所有学生信息
StudentDao studao = new StudentDao();
List<Student> stus = studao.queryAllStus("");
//打印流
PrintWriter out = response.getWriter();
out.write("<html><body><h1 align='center'>学生信息管理系统</h1>" +
"<div align='center'><form action='selstuat' method='get'>" +
"<input type='text' name='sName'>" +
"<button type='submit'>查询</button></form></div>" +
"<table border='1' cellspacing='0' cellpadding='10' align='center'>");
out.write("<tr><td>"+"学生编号"
+"</td><td>"+"学号"
+"</td><td>"+"学校"
+"</td><td>"+"姓名"
+"</td><td>"+"昵称"
+"</td><td>"+"手机号"
+"</td><td>"+"生日"
+"</td><td>"+"添加操作"
+"</td><td>"+"删除操作"
+"</td><td>"+"修改操作"
+"</td></tr>");
for (int i = 0; i < stus.size(); i++) {
Student stu = stus.get(i);
out.write("<tr><td>"+stu.getsId()
+"</td><td>"+stu.getsNum()
+"</td><td>"+stu.getsSchool()
+"</td><td>"+stu.getsName()
+"</td><td>"+stu.getsNickname()
+"</td><td>"+stu.getsTel()
+"</td><td>"+stu.getsBDay()
+"</td><td>"+"<a href='register.jsp' style='text-decoration:none'>添加</a>"
+"</td><td>"+"<a href='/delstu?id=" +stu.getsId() + "' style='text-decoration:none'>删除</a>"
+"</td><td>"+"<a href='/updstu?sId=" +stu.getsId() + "&sNum=" + stu.getsNum()
+ "&sSchool=" + stu.getsSchool() + "&sName=" + stu.getsName()
+ "&sNName=" + stu.getsNickname() + "&sTel=" + stu.getsTel()
+ "&sBDay=" + stu.getsBDay() + "' style='text-decoration:none'>修改</a>"
+"</td></tr>");
}
out.write("</table></body></html>");
}
}
实现增加功能直接跳转注册页面,注册成功直接跳转selstu
package com.beizhen.servlet;
import com.beizhen.bean.Student;
import com.beizhen.dao.StudentDao;
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 java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/registerServlet")
public class RegisterServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置请求和响应编码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//获取请求参数
String sNum = request.getParameter("sNum");
String sSchool = request.getParameter("sSchool");
String sName = request.getParameter("sName");
String sNickname = request.getParameter("sNickname");
String sTel = request.getParameter("sTel");
String sBDay = request.getParameter("sBDay");
Student stu = new Student(0,sNum,sSchool,sName,sNickname,sTel,sBDay);
int i = new StudentDao().addStu(stu);
PrintWriter out = response.getWriter();
if(i != 0){
out.write("<script type='text/javascript'>");
out.write("alert('恭喜你注册成功!');");
out.write("alert('即将跳转学生信息管理!');");
out.write("window.location='selstu';");
out.write("</script>");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
实现删除功能,通过get请求向服务器发送url获得id
<a href='/delstu?id=" +stu.getsId() + "' style='text-decoration:none'>删除</a>
package com.beizhen.servlet;
import com.beizhen.bean.Student;
import com.beizhen.dao.StudentDao;
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 java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/delstu")
public class DelStuServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置请求和响应编码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//获取请求参数
int id = Integer.parseInt(request.getParameter("id"));
int num = new StudentDao().deleteStu(id);
PrintWriter out = response.getWriter();
if(num != 0){
out.write("<script type='text/javascript'>");
out.write("alert('恭喜你删除成功!');");
out.write("window.location='selstu';");
out.write("</script>");
} else {
out.write("<script type='text/javascript'>");
out.write("alert('删除失败!');");
out.write("window.location='selstu';");
out.write("</script>");
}
}
}
实现修改功能,同样通过get请求向服务器发送url获得id等其他信息,同时学号不能编辑
//学号不可编辑
readonly unselectable='on'
package com.beizhen.servlet;
import com.beizhen.bean.Student;
import com.beizhen.dao.StudentDao;
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 java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/updstu")
public class UpdStuServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置请求和响应编码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//获取请求参数
int sId = Integer.parseInt(request.getParameter("sId"));
String sNum = request.getParameter("sNum");
String sSchool = request.getParameter("sSchool");
String sName = request.getParameter("sName");
String sNickname = request.getParameter("sNickname");
String sTel = request.getParameter("sTel");
String sBDay = request.getParameter("sBDay");
Student stu = new Student(sId,sNum,sSchool,sName,sNickname,sTel,sBDay);
int i = new StudentDao().updateStu(stu);
PrintWriter out = response.getWriter();
if(i != 0){
out.write("<script type='text/javascript'>");
out.write("alert('恭喜你修改成功!');");
out.write("window.location='selstu';");
out.write("</script>");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置请求和响应编码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//打印流
PrintWriter out = response.getWriter();
out.write("<html><body>");
out.write("<form action='updstu' method='post'>");
//学号不可编辑
out.write("ID:<input type='text' name='sId' readonly unselectable='on' value='" + request.getParameter("sId") + "'><br>");
out.write("学号:<input type='text' name='sNum' readonly unselectable='on' value='" + request.getParameter("sNum") + "'><br>");
out.write("学校:<input type='text' name='sSchool' value='" + request.getParameter("sSchool") + "'><br>");
out.write("姓名:<input type='text' name='sName' readonly unselectable='on' value='" + request.getParameter("sName")+ "' required><br>");
out.write("昵称:<input type='text' name='sNickname' value='" + request.getParameter("sNName") + "'><br>");
out.write("电话:<input type='text' name='sTel' value='" + request.getParameter("sTel") + "'><br>");
out.write("生日:<input type='text' name='sBDay' value='" + request.getParameter("sBDay") + "'><br>");
out.write("<button type='submit'>修改</button>");
out.write("</form><html><body>");
}
}
实现查询功能,根据名字查询
package com.beizhen.servlet;
import com.beizhen.bean.Student;
import com.beizhen.dao.StudentDao;
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 java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@WebServlet("/selstuat")
public class SelStuAtServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置请求和响应编码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//获得学生请求信息
String sName = request.getParameter("sName");
//获得数据库所有学生信息
StudentDao studao = new StudentDao();
List<Student> stus = studao.queryAllStus(sName);
//打印流
PrintWriter out = response.getWriter();
out.write("<html><body><h1 align='center'>学生信息管理系统</h1>" +
"<div align='center'><form action='selstuat' method='get'>" +
"<input type='text' name='sName'>" +
"<button type='submit'>查询</button></form></div>" +
"<table border='1' cellspacing='0' cellpadding='10' align='center'>");
out.write("<tr><td>"+"学生编号"
+"</td><td>"+"学号"
+"</td><td>"+"学校"
+"</td><td>"+"姓名"
+"</td><td>"+"昵称"
+"</td><td>"+"手机号"
+"</td><td>"+"生日"
+"</td><td>"+"添加操作"
+"</td><td>"+"删除操作"
+"</td><td>"+"修改操作"
+"</td></tr>");
for (int i = 0; i < stus.size(); i++) {
Student stu = stus.get(i);
out.write("<tr><td>"+stu.getsId()
+"</td><td>"+stu.getsNum()
+"</td><td>"+stu.getsSchool()
+"</td><td>"+stu.getsName()
+"</td><td>"+stu.getsNickname()
+"</td><td>"+stu.getsTel()
+"</td><td>"+stu.getsBDay()
+"</td><td>"+"<a href='register.jsp' style='text-decoration:none'>添加</a>"
+"</td><td>"+"<a href='/delstu?id=" +stu.getsId() + "' style='text-decoration:none'>删除</a>"
+"</td><td>"+"<a href='/updstu?sId=" +stu.getsId() + "&sNum=" + stu.getsNum()
+ "&sSchool=" + stu.getsSchool() + "&sName=" + stu.getsName()
+ "&sNName=" + stu.getsNickname() + "&sTel=" + stu.getsTel()
+ "&sBDay=" + stu.getsBDay() + "' style='text-decoration:none'>修改</a>"
+"</td></tr>");
}
out.write("</table></body></html>");
}
}