JavaEE学习日志持续更新----> 必看!JavaEE学习路线(文章总汇)
JavaEE学习日志(五十一)
登陆案例
登陆案例的流程
创建数据库表
CREATE DATABASE web02;
USE web02;
CREATE TABLE `user`(
username VARCHAR(50),
`password` VARCHAR(50)
);
INSERT INTO `user` VALUES('tom','123'),('jerry',456);
创建一个新的module:Web02_login,并配置好tomcat和运行路径
添加jar包
添加c3p0配置文件和c3p0Utils工具类
创建javabean和servlet包
代码实现
简单的前端登录
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登陆</title>
</head>
<body>
<!--
action提交服务器路径
Web的module名/servlet访问的虚拟路径
-->
<form method="post" action="/Web02_login/login">
用户名<input type="text" name="username" placeholder="请输入用户名"><br>
密码<input type="password" name="password" placeholder="请输入密码"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
实现登陆的servlet程序步骤
- 获取表单提交的数据
- 作为数据表的查询条件
- 获取查询很多结果集
- 结果集判断,响应浏览器,成功还是失败
获取表单提交的数据
request:负责接收客户端提交的数据
方法:String getParameter("表单的name属性值")
返回表单填写的内容
import com.itheima.domain.User;
import com.itheima.utils.C3P0UtilsXML;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取表单提交的数据
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username+"="+password);
//作为数据表的查询条件
//创建dbutils工具的核心类对象QueryRunner
QueryRunner qr = new QueryRunner(C3P0UtilsXML.getDataSource());
//登陆查询的sql
String sql="SELECT * FROM user WHERE username=? AND password = ?;";
//执行sql,获取查询后的结果集BeanHandler
User user = null;
try {
user = qr.query(sql, new BeanHandler<User>(User.class), username, password);
} catch (Exception e) {
e.printStackTrace();
}
//对查询后的结果集进行判断
//条件不匹配,查询不到任何数据,返回null
if(user==null){
//查询不到数据,登录失败
response.getWriter().print("login error");
}else{
//登录成功
response.getWriter().print("login success");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
结果
ServletContext对象
概述
服务器中的项目,名称叫做WEB应用程序,应用程序也是对象
ServletContext对象表示WEB应用程序
一个WEB应用程序,只能存在一个ServletContext对象,具有唯一性
ServletContext是一个接口,接口的实现类是tomcat引擎提供
ServletContext对象获取
获取方法:
- ServletConfig对象的方法
getServletContext()
- 继承HttpServlet类,又继承GenericServlet类定义方法
getServletContext()
ServletContext context = getServletContext();
ServletContext对象的作用
获取Web应用程序的初始化参数:从web.xml进行配置
<!--配置的是WEB程序的初始化参数-->
<context-param>
<param-name>heima</param-name>
<param-value>java</param-value>
</context-param>
//获取配置文件中的初始化参数
String value = context.getInitParameter("heima");
System.out.println(value);
获取WEB应用程序下任意资源的绝对路径
方法:String getRealPath("资源相对路径")
在Web03的不同路径下,创建了a.txt,b.txt,c.txt,d.txt
4个文件
获取他们
//获取web目录下的a.txt文件的绝对路径
String aPath = context.getRealPath("a.txt");
System.out.println(aPath);//D:\Program Files\tomcat\apache-tomcat-8.5.32\webapps\Web03\a.txt
//获取web目录下WEB-INF目录下的b.txt文件的绝对路径
String bPath = context.getRealPath("WEB-INF/b.txt");
System.out.println(bPath);//D:\Program Files\tomcat\apache-tomcat-8.5.32\webapps\Web03\WEB-INF\b.txt
//获取src目录下的c.txt文件的绝对路径
String cPath = context.getRealPath("WEB-INF/classes/c.txt");
System.out.println(cPath);//D:\Program Files\tomcat\apache-tomcat-8.5.32\webapps\Web03\WEB-INF\classes\c.txt
//获取Web03module下的d.txt文件的绝对路径,获取不到
域对象
ServletContext对象是一个容器,可以存储数据
对象有个作用域问题:ServletContext对象的作用域是整个WEB应用程序
方法
- 向域对象存储数据:
setAttribute(String key, Object value)
- 取出域对象数据:
Object getAttribute(String key)
- 移除域对象数据:
removeAttribute(String key)
public class Context2Servlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
域对象的存储和取出
*/
ServletContext context = getServletContext();
/*
域对象存储数据,键值对
*/
context.setAttribute("heima","java");
//取出
Object obj = context.getAttribute("heima");
System.out.println(obj);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
ServletContext对象的空指针异常
ServletContext对象通过父类的方法getServletContext()获取
**原理:**记结论就行了
结论:如果要重写init,则不能加参数
统计访问的次数
练习域对象ServletContext的使用
做法:
- 第一次访问Servlet的时候,把数据1存储到域对象
- 第二次访问,从域中取出数据++,再存储回去
public class CountServlet extends HttpServlet {
@Override
public void init() throws ServletException {
ServletContext context = getServletContext();
context.setAttribute("count",1);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
Integer count = (Integer)context.getAttribute("count");
response.getWriter().print("welcome"+count);
count++;
context.setAttribute("count",count);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
注解开发取代web.xml
格式:@WebServlet
使用方式:把注解添加到自己定义的Servlet中的类的声明上即可
注解的属性:urlPatterns
,属性值就是浏览器的访问值
@WebServlet(urlPatterns = "/test")