实现登陆功能:
登陆页面(用户名和密码要能传到后台)index.jsp
servlet接收数据–>数据传递给service–>service经过合法性判断–>调用dao的查询方法–>dao查询返回数据给service–>service经过业务逻辑判断–>把结果传回给servlet–>传回给页面
8.25课前复习:
1、jdbc连接数据库的两种步骤
加载驱动:Class.forName(“com.mysql.jdbc.Driver”);
获取连接:Connection conn=DriverManager.getConnection(url,user,password)
statement:
创建语句管理对象:Statement stmt=conn.getStatement();
装载sql并运行:
ResultSet rs=stmt.executeQuery(sql);
int num=stmt.executeUpdate(sql);
PreparedStatement
PreparedStatement pst=conn.prepareStatement(sql);
ResultSet rs=pst.executeQuery();
int num=pst.executeUpdate();
遍历结果集rs
关闭资源
2、数据库配置使用配置文件存储的方式
Properties p=new Properties();
p.load(new FileInputStream(配置文件路径));
String 属性名 =p.get(属性名);
3、Java三层模型是指哪三层分别什么作用
dao:与数据库交互
service:业务逻辑的判断
controller:与前端页面的交互
配置web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>cn.kgc.kb09.controller.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<!--/login.html /不要丢-->
<url-pattern>/login.html</url-pattern>
</servlet-mapping>
</web-app>
初始页面index.jsp:
<%--
Created by IntelliJ IDEA.
User: chaokeaimuzhi
Date: 2020/8/22
Time: 14:01
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
</head>
<body>
<h1>欢迎来到吃货联盟</h1>
<form action="login.html" method="get">
<p><input type="text" name="username"></p>
<p><input type="password" name="password"></p>
<p><input type="submit" name="点我登陆"></p>
</form>
</body>
</html>
cn.kgc.kb09.controller.LoginServlet:
package cn.kgc.kb09.controller;
import cn.kgc.kb09.service.LoginService;
import cn.kgc.kb09.service.LoginServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @Author: ChaoKeAiMuZhi
* @Date: 2020/8/22 15:22
* @Description:
**/
public class LoginServlet extends HttpServlet {
private LoginService service;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password= req.getParameter("password");
System.out.println(username);
System.out.println(password);
service=new LoginServiceImpl();
boolean canLogin = service.login(username, password);
if(canLogin){
//可以登陆
req.setAttribute("rst","恭喜"+username+",登陆成功");
}else{
//不可以登陆
req.setAttribute("rst","对不起,输入有误,登陆不成功");
}
//转发请求到新的页面
req.getRequestDispatcher("result.jsp").forward(req,resp);
}
}
LoginService接口 :
package cn.kgc.kb09.service;
/**
* @Author: ChaoKeAiMuZhi
* @Date: 2020/8/22 16:05
* @Description:
**/
public interface LoginService {
boolean login(String username,String password);
}
LoginServiceImpl实现类:
package cn.kgc.kb09.service;
import cn.kgc.kb09.dao.LoginDao;
import cn.kgc.kb09.dao.LoginDaoImpl;
import cn.kgc.kb09.entity.User;
/**
* @Author: ChaoKeAiMuZhi
* @Date: 2020/8/22 16:06
* @Description:
**/
public class LoginServiceImpl implements LoginService {
LoginDao dao;
@Override
public boolean login(String username, String password) {
dao=new LoginDaoImpl();
//合法性判断
if(username==null || password==null || username.trim().equals("")
|| password.trim().equals("")){
return false;
}
//调用dao层的数据查询
User user =dao.queryUserByNameAndPwd(username,password);
if(user!=null){
return true;
}
return false;
}
}
LoginDao接口:
package cn.kgc.kb09.dao;
import cn.kgc.kb09.entity.User;
/**
* @Author: ChaoKeAiMuZhi
* @Date: 2020/8/22 15:05
* @Description:
**/
public interface LoginDao {
User queryUserByNameAndPwd(String username,String password);
}
LoginDaoImpl实现类:
package cn.kgc.kb09.dao;
import cn.kgc.kb09.entity.User;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @Author: ChaoKeAiMuZhi
* @Date: 2020/8/22 15:05
* @Description:
**/
public class LoginDaoImpl implements LoginDao{
@Override
public User queryUserByNameAndPwd(String username, String password) {
String sql="select * from user_info where uname=? and password=?";
ResultSet rs = PstDao.query(sql, username, password);
User user=null;
try{
if(rs.next()){
user=new User();
user.setUsername("uname");
user.setPassword("password");
}
} catch (SQLException e) {
e.printStackTrace();
}
return user;
}
}
PstDao实现连接数据库查询:
package cn.kgc.kb09.dao;
import cn.kgc.kb09.util.Prop;
import java.sql.*;
/**
* @Author: ChaoKeAiMuZhi
* @Date: 2020/8/22 15:08
* @Description:
**/
public class PstDao {
public static Connection getconn(){
Prop p = new Prop();
String driver=p.getP("driver");
String url=p.getP("url");
String user=p.getP("user");
String pwd=p.getP("pwd");
try {
Class.forName(driver);
return DriverManager.getConnection(url,user,pwd);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static ResultSet query(String sql,Object... param){
ResultSet rs =null;
Connection conn=getconn();
try {
PreparedStatement pst = conn.prepareStatement(sql);
for (int i = 0; i < param.length; i++) {
pst.setObject(i+1,param[i]);
}
rs=pst.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
}
Prop:util类,配置properties文件,在Java web项目中使用绝对路径
package cn.kgc.kb09.util;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
/**
* @Author: ChaoKeAiMuZhi
* @Date: 2020/8/22 14:07
* @Description:
**/
public class Prop {
Properties p=new Properties();
public String getP(String param){
try {
p.load(new FileInputStream("E:\\softs\\idea_datas\\ideaProjects\\20200822-practise\\resources\\db.properties"));
} catch (IOException e) {
e.printStackTrace();
}
return p.getProperty(param);
}
}
db.properties 配置文件
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.237.100:3306/userControl
user=root
pwd=ok
package cn.kgc.kb09.entity;
/**
* @Author: ChaoKeAiMuZhi
* @Date: 2020/8/22 15:03
* @Description:
**/
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
result.jsp返回的结果页面 <% 其中可以写Java代码 %>
<%--
Created by IntelliJ IDEA.
User: chaokeaimuzhi
Date: 2020/8/22
Time: 11:45
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="Java" %>
<html>
<head>
<title>登陆结果页面</title>
</head>
<body>
<%
Object rst=request.getAttribute("rst");
%>
<h1><%=rst%></h1>
</body>
</html>
Tomcat相关配置设置:
1.Tomcat目录地址:
D:\softs\apache-tomcat-8.5.45
2.在idea中配置tomcat
File->Project Structure->Modules->+选择web->apply
点击右下角create artifact->apply
然后回到Modules->选择项目名->选择dependencies添加依赖->Tomcat8.5.45 ok
3.配置项目Configurations
run->Edit Configurations->
On update action:当发现更新时的操作 选择Update classes and resources
On frame deactivation:当IDEA 切换时的操作 (比如最小化、打开网页等) 选择Update classes and resources
选择Deployment->选择+ Artifact ->
2020.8.27补充:
public class Prop {
static Properties p=new Properties();
public static String getP(String param){
try {
/*
说明:
1、点(.):代表java命令运行的目录;
2、斜杠(/):表示classpath的根目录;
在java项目下,点(.) java命令的运行目录从项目的根目录开始的;
classpath的根目录从bin目录开始的
在web项目下,点(.) java命令的运行目录从tomcat/bin目录下开始的;
classpath的根目录从web-INF/classes目录下开始的
*/
p.load(Prop.class.getClassLoader().getResourceAsStream("db.properties"));
//p.load(new FileInputStream("E:\\softs\\idea_datas\\ideaProjects\\20200821-web\\resources\\db.properties"));
} catch (IOException e) {
e.printStackTrace();
}
return p.getProperty(param);
}
}
采用绝对路径的方式非常,不友好,在导出war包到服务器端的时候,会出现找不到路径的情况,这里采用p.load(Prop.class.getClassLoader().getResourceAsStream(“db.properties”))的方式,要求:resources文件夹为resource资源文件夹类型,并且resources在src目录下
如何将一个web项目打成war包:
file->project sturcture->artifacts->"+" web application:archive->选择第二个"for:xxx:war exploded"->apply ok
Build->build artifacts->选择刚才生成的xxx:war->build->在out/artifacts/xxx_war/xxx_war.war找到war包
上传war包的目录:
在虚拟机tomcat文件夹目录下->webapps文件夹
启动tomcat->bin目录下 ./startup.sh
然后浏览器访问
ip地址:8080端口号/war包项目名称/