javaweb--demo小项目
。。。。。。。。数据库准备:表准备
java数据库配置连接《JDBC准备》:
Druid工具类准备:
package com.gton.servlet.utils; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.sql.*; import java.util.Properties; /** * @program: Jdbc-start * @description: Druid工具类 * @author: GuoTong * @create: 2020-09-01 15:23 **/ public class DruidUtils { //初始化连接池 static DataSource dataSource; static { //创建druid连接池 try { Properties properties = new Properties(); properties.load(DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties")); dataSource = DruidDataSourceFactory.createDataSource(properties); } catch (Exception e) { e.printStackTrace(); } } public static DataSource getDataSource(){ return dataSource; } public static Connection getConnection(){ try { return dataSource.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return null; } //关闭资源 public static void close(Connection conn, Statement stmt) { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(Connection conn, PreparedStatement stmt) { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(Connection conn, Statement stmt, ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } close(conn, stmt); } public static void close(Connection conn, PreparedStatement stmt, ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } close(conn, stmt); } }
DB.properties准备:
。。。。。实体类:
package com.gton.servlet.entity; /** * @program: javaweb-tomcat * @description: * @author: GuoTong * @create: 2020-09-14 16:09 **/ public class Student { private int id; private String name; private String pwd; public Student() { } public Student(int id, String name, String pwd) { this.id = id; this.name = name; this.pwd = pwd; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", pwd='" + pwd + '\'' + '}'; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
DAO层开发:
package com.gton.servlet.dao; import com.gton.servlet.entity.Student; import com.gton.servlet.utils.DruidUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; /** * @program: javaweb-tomcat * @description: * @author: GuoTong * @create: 2020-09-14 16:11 **/ public class StudentDao { public List<Student> findByPwdAndName(String name, String pwd) { Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; List<Student> list=new ArrayList<>(); try { //1.获取连接 connection = DruidUtils.getConnection(); //2 第二部sql String sql = "select * from student where name =? and pwd =?"; //获取sql操作对象 preparedStatement = connection.prepareStatement(sql); //设置参数 preparedStatement.setString(1, name); preparedStatement.setString(2, pwd); //执行sql resultSet = preparedStatement.executeQuery(); //接收参数 while (resultSet.next()) { Student student = new Student(); student.setId(resultSet.getInt("id")); student.setName(resultSet.getString("name")); student.setPwd(resultSet.getString("pwd")); list.add(student); } if (list.size()>0){ System.out.println("查询成功,登录正确"); return list; }else { System.out.println("检查账号密码,登录失败"); return null; } } catch (SQLException e) { e.printStackTrace(); } finally { DruidUtils.close(connection, preparedStatement, resultSet); } return null; } }
Service层开发:
package com.gton.servlet.service; import com.gton.servlet.dao.StudentDao; import com.gton.servlet.entity.Student; import java.util.List; /** * @program: javaweb-tomcat * @description: * @author: GuoTong * @create: 2020-09-14 16:25 **/ public class StudentService { private StudentDao studentDao =new StudentDao(); public List<Student> findByPwdAndName(String name,String pwd){ return studentDao.findByPwdAndName(name,pwd); } }
Servlet开发:
package com.gton.servlet; import com.gton.servlet.entity.Student; import com.gton.servlet.service.StudentService; import org.omg.CORBA.Request; 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.time.chrono.IsoChronology; import java.util.List; import java.util.Map; import java.util.Set; /** * @program: javaweb-tomcat * @description: * @author: GuoTong * @create: 2020-09-14 16:00 **/ @WebServlet(urlPatterns = {"/login"}) public class LoginServlet extends HttpServlet { /** * *response 设置状态码:setStatus(int sc) */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); resp.setCharacterEncoding("UTF-8"); req.setCharacterEncoding("UTF-8"); String name =req.getParameter("user"); String pwd =req.getParameter("pwd"); if (name!=null && pwd !=null){ List<Student> byPwdAndName = new StudentService().findByPwdAndName(name, pwd); if (byPwdAndName.size()>=0){ req.setAttribute("name",name); req.getRequestDispatcher("/success").forward(req,resp); }else { req.setAttribute("name","您输入的用户名有问题"); req.getRequestDispatcher("/fail").forward(req,resp); } }else { System.out.println("错误,"+name+":"+pwd); } } }
Servlet开发:
package com.gton.servlet; import com.gton.servlet.entity.Student; import com.gton.servlet.service.StudentService; import org.omg.CORBA.Request; 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.time.chrono.IsoChronology; import java.util.List; import java.util.Map; import java.util.Set; /** * @program: javaweb-tomcat * @description: * @author: GuoTong * @create: 2020-09-14 16:00 **/ @WebServlet(urlPatterns = {"/login"}) public class LoginServlet extends HttpServlet { /** * *response 设置状态码:setStatus(int sc) */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); resp.setCharacterEncoding("UTF-8"); req.setCharacterEncoding("UTF-8"); String name =req.getParameter("user"); String pwd =req.getParameter("pwd"); if (name!=null && pwd !=null){ List<Student> byPwdAndName = new StudentService().findByPwdAndName(name, pwd); if (byPwdAndName.size()>=0){ req.setAttribute("name",byPwdAndName.get(0).getName()); req.getRequestDispatcher("/success").forward(req,resp); }else { req.setAttribute("name","您输入的用户名或者密码有问题"); req.getRequestDispatcher("/fail").forward(req,resp); } }else { System.out.println("错误,"+name+":"+pwd); } } }
成功的Servlet:
package com.gton.servlet; 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; /** * @program: javaweb-tomcat * @description: * @author: GuoTong * @create: 2020-09-14 16:02 **/ @WebServlet(urlPatterns = {"/success"}) public class SuccessServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); resp.setCharacterEncoding("UTF-8"); req.setCharacterEncoding("UTF-8"); String name = (String) req.getAttribute("name"); PrintWriter writer = resp.getWriter(); writer.print(name + "欢迎您"); writer.flush(); writer.close(); } }
失败的servlet:
package com.gton.servlet; 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; /** * @program: javaweb-tomcat * @description: ${description} * @author: GuoTong * @create: 2020-09-14 16:05 **/ @WebServlet(urlPatterns = {"/fail"}) public class FialServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); String name = (String) request.getAttribute("name"); PrintWriter writer = response.getWriter(); writer.print(name); writer.flush(); writer.close(); } }
前端的入口页面:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/login" method="post"> <label for="isuser">用户名</label> <input type="text" name="user" id="isuser"/> <br> <label for="isuser">密码</label> <input type="password" name="pwd" id="ispwd"/> <br> <input type="submit" value="提交"> </form> </body> </html>
作者:隔壁老郭
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
Java入门到入坟
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南