三个重写方法
1.Servlet的 init()方法:
默认情况下:当第一次访问该Servlet时,被调用(该Servlet被初始化),且只被调用一次【public void init() throws ServletException{
super.init();】(第一次访问只被调用一次)
2.Servlet的 servlet()方法:
servlet的主方法,每访问一次就被调用一次
【protected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException】
3.Servlert的destroy()方法:
【public void destroy() 】
在服务器被关闭时,被调用(该Servlet被销毁)
package com.yy.servlet.web.servlet;
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;
import javax.xml.ws.Response;
public class FirstServlet extends HttpServlet{
@Override
public void init() throws ServletException{
super.init();
System.out.println("第一次启动只调用一次");
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("访问一次调用一次");
}
@Override
public void destroy() {
super.destroy();
System.out.println("被摧毁");
}
}
===========================================
Servlert也是单例模式,Servlert只被初始化一次
===========================================
通过Servlert判断接收的参数是否存在数据库
package com.yy.servlet.web.servlet;
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;
import java.util.HashMap;
import java.util.Map;
import com.yy.servlet.dao.UserDao;
import com.yy.servlet.po.User;
public class FirstServlet extends HttpServlet {
@Override
public void init() throws ServletException {
System.out.println("FirstServlet这个servlet被初始化");
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//接收参数
String username = request.getParameter("username");
String password = request.getParameter("password");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("用户名"+username);
out.print("密码"+password);
UserDao userDao = UserDao.getInstance();
Map
map = new HashMap
();
map.put("username", username);
map.put("password",password);
User user =userDao.getUserByMap(map);
if(user !=null){
out.print("在数据库中存在");
}else{
out.print("数据库中不存在");
}
out.flush();
out.close();
}
@Override
public void destroy() {
System.out.println("FirstServlet这个servlet被摧毁");
}
}
网页效果如下图
==========================================================
HttpServletRequest接口
获取请求的基本信息
1.getMethod()方法:返回客户端请求方式
2.getRequestURL()方法:返回端口后参数之前那部分内容,且为真实的资源名,而不一定与地址栏中相同
3.getQueryString()方法:返回资源路径?号后面所有的内容
4.getScheme()方法:返回协议名称
5.getContextPath()方法:返回情切url所属的web应用程序的路径
6.getrequestURL()方法:返回真实的资源路径,不一定与地址栏相同,但不包括参数部分,且返回类型SrtingBuffer
标准Servlert的写法:
1.新建一个Servlert
2.写上名字,选择
doget()方法和
doPost()方法
3.点next 给servlet配上在web.xml文件中的名字和URL,点finish 会在web.xml文件中自动配置Servlert
不需要
service()方法
实际上 service()方法就是先判断,请求的方式
get方式:Service()方法就调用
doget()方法
psot方式:Service()方法就调用
doPost()方法
========================================================
用servlet写一个注册登录的用户例子
第一个使用jsp文件建一个登录开头
登录页面
不存在就会弹出注册 然后跳转
注册成功后 数据会保存在数据库
package com.yy.servlet.web.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.yy.servlet.dao.UserDao;
import com.yy.servlet.po.User;
public class LonginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
PrintWriter out = response.getWriter();
out.print("用户名"+username);
out.print("密码"+password);
UserDao userDao = UserDao.getInstance();
Map
map = new HashMap
();
map.put("username", username);
map.put("password",password);
User user =userDao.getUserByMap(map);
if(user != null){
out.print("登陆成功");
}else{
out.print("用户名不存在,请注册");
out.print("
");
}
out.flush();
out.close();
}
}
package com.yy.servlet.web.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.yy.servlet.dao.UserDao;
import com.yy.servlet.po.User;
public class NextServlet extends HttpServlet{
@Override
public void init()throws ServletException{
super.init();
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
String username = request.getParameter("username");
String password = request.getParameter("password");
UserDao userDao=UserDao.getInstance();
PrintWriter out = response.getWriter();
out.print("用户名"+username);
out.print("密码"+password);
Map
map= new HashMap
();
map.put("username",username);
map.put("password",password);
User user =userDao.getUserByMap(map);
if(user != null){
out.print("用户名已存在,请登录
");
out.print("
");
}else{
User user2 = new User();
user2.setUsername(username);
user2.setPassword(password);
boolean flag = UserDao.getInstance().saveUser2(user2);
if(flag){
out.print("OK");
}else{
out.print("NO");
}
}
out.flush();
out.close();
}
}
public User getUserByMap(Map
map){
User user = null;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from user where username = ? ";
try {
conn = DBconnectionUitl.getConnection();
ps = conn.prepareStatement(sql);
ps.setString(1, map.get("username"));
rs = ps.executeQuery();
while(rs.next()){
user = new User();
user.setUserId(rs.getInt("userId"));
user.setUsername(rs.getString("username"));
user.setSex(rs.getInt("sex"));
user.setPassword(rs.getString("password"));
user.setFlag(rs.getInt("flag"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBconnectionUitl.closeAll(rs, ps, conn);
}
return user;
}
//查询数据库中是否存在数据
public boolean saveUser2 (User user2){
boolean flag = false;
Connection conn = null;
PreparedStatement ps = null;
String sql = "insert into user (username,password) value (?,?)";
try {
conn = DBconnectionUitl.getConnection();
ps = conn.prepareStatement(sql);
ps.setString(1,user2.getUsername());
ps.setString(2,user2.getPassword());
int rows =ps.executeUpdate();
if(rows != 0)
flag =true;
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBconnectionUitl.closeAll(null,ps,conn);
}
return flag;
}
} //注册时调用的插入语句