• 采用JSP+Servlet+JavaBean+JDBC方式开发一个web登录程序


    1. 开发环境:Oracle12、JDK1.8、Tomcat9.0、Myeclipse 2020-12(4.18.0)

    2. 开发框架:

    模型:beans包和DAO包

    视图:jsp页面

    控制器:servlets

     3. 建立数据库与表,创建数据库连接

    3.1 创建数据库实例ORCL,表空间,用户名aml/aml,表。

    表1.1 userTable 表
    字段名称 数据类型 主键 自增 允许为空 描述
    id int yes 增1   ID号
    username varchar(20)       用户名
    password varchar(20)       密码

     3.2 创建数据库连接

    • 加载JDBC驱动包:ojdbc7.jar,可从网上下载并保存在Myeclipse 2014默认工作区“C:UserjoinWorkspacesMyeElipse professional 2014(C:UsersAdministrator.A9K9MO0KFT1XUQOeclipse-workspace),同时,将JDBC驱动包ojdbc7.jar复制到项目"WebRootWEB-INFlib"目录下。
    • 创建数据源连接:主菜单【Window】,选择【Open Perspective】,【Database Development】,右击Database Connections选择【New...】,编辑数据库驱动如图1.3,其中Driver JARs 即先前存盘的数据库驱动。右击创建的sqlsrv,选择菜单【Open connection...】测试

     4. 详细开发步骤

    (1)创建Java EE项目:选择主菜单【File】【New】【Web Project】,在【New Dynamic Web Project】窗口中填写项目名jsp_servlet_javabean_jdbc,单击【Next】,在“Web Module"页勾选"Generate web.xml deployment descriptor",可自动生成web.xml配置文件。单击【Next】,勾选"JavaEE 7.0 Generic Library",取消选择''JSTL 1.2.2 Library(除去不必要的类库)。设置完成后,单击【Finish】,生成一个Java EE 项目。

    (2)创建JDBC类:右击项目src,选择【New】【Package】,输入包名"org.easybooks.test.jdbc",单击【Finish】按钮。

    右击src,选择【New】【Class】,指定"org.easybooks.test.jdbc"包,输入类名"OracleDBConn"。

    OracleDBConn.java代码如下:

    package org.easybooks.test.jdbc;
    import java.sql.*;
    public class OracleDBConn {
    private Statement stmt;
    private Connection conn;
    ResultSet rs;
    public OracleDBConn(){
    stmt=null;
    try{
    Class.forName("oracle.jdbc.driver.OracleDriver");
    //装载oracle的driver类,datacore.driver=oracle.jdbc.driver.OracleDriver
    conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/ORCL","aml","aml");

    }catch(Exception e){
    e.printStackTrace();
    }
    rs=null;
    }
    //执行查询类语句,又返回集
    public ResultSet executeQuery(String sql)
    {
    try{
    stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
    ResultSet.CONCUR_UPDATABLE);
    rs=stmt.executeQuery(sql);
    }catch(SQLException e){
    System.err.println("Data.executeQuery:"+e.getMessage());
    }
    return rs;
    }
    //关闭对象
    public void closeStmt()
    {
    try{
    stmt.close(); //关闭Statement对象
    }catch(SQLException e){
    System.err.println("Data.executeQuery:"+e.getMessage());
    }
    }
    public void closeConn()
    {
    try{
    conn.close(); //关闭连接
    }catch(SQLException e){
    System.err.println("Data.executeQuery:"+e.getMessage());
    }
    }
    }

     (3)构造JavaBean:在src下建立包org.easybooks.test.model.vo,其中创建名为UserTable的类,为userTable表构造一个JavaBean,代码如下:

    package org.easybooks.test.model.vo;

    public class UserTable {
    private Integer id;
    private String username;
    private String password;
    public Integer getId() {
    return this.id;
    }
    public void setId(Integer id) {
    this.id = id;
    }
    public String getUsername() {
    return this.username;
    }
    public void setUsername(String username) {
    this.username = username;
    }
    public String getPassword() {
    return this.password;
    }
    public void setPassword(String password) {
    this.password = password;
    }

    }

    (4)编写JSP:

    展开项目工程树,右击"WebRoot",选择【New】
ightarrow【File】,在File name中输入文件名login.jsp,单击【Finish】。在代码编辑器中编写登录页login.jsp文件。代码如下:

    <%@ page language="java" pageEncoding="utf-8"%>
    <html>
    <head><title>登录</title></head>
    <body bgcolor="#E3E3E3">
    <form action="mainServlet" method="post">
    <table>
    <caption>用户登录:</caption>
    <tr>
    <td>用户名:</td>
    <td>
    <input type="text" name="username" size="20"/>
    </td>
    </tr>
    <tr>
    <td>密码:</td>
    <td>
    <input type="password" name="password" size="21"/>
    </td>
    </tr>
    </table>
    <input type="submit" value="登录"/>
    <input type="reset" value="重置"/>
    </form>
    如果没注册单击<a href="">这里</a>注册!
    </body>
    </html>

    此页面用于登录首页。同样方法在WebRoot下创建主页main.jsp文件,代码如下:

    <%@page language="java" pageEncoding="gb2312" import="org.easybooks.test.model.vo.UserTable"%>
    <html>
    <head>
    <title>留言板信息</title>
    </head>
    <body>
    <%
    UserTable user=(UserTable)session.getAttribute("user");
    String usr=user.getUsername();

    %>
    <%=usr%>,您好,欢迎登录留言板。
    </body>
    </html>

     同样方法在WebRoot下创建主页error.jsp文件,代码如下:

    package org.easybooks.test.servlet;
    import java.sql.*;
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import org.easybooks.test.jdbc.OracleDBConn;
    import org.easybooks.test.model.vo.UserTable;;
    public class MainServlet extends HttpServlet {
    public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,
    IOException{
    request.setCharacterEncoding("gb2312");
    String usr=request.getParameter("username");
    String pwd=request.getParameter("password");
    boolean validated=false;
    OracleDBConn sqlsrvdb=new OracleDBConn();
    HttpSession session=request.getSession();
    UserTable user=null;
    user=(UserTable)session.getAttribute("user");
    if(user==null){
    String sql="select * from userTable";
    ResultSet rs=sqlsrvdb.executeQuery(sql);
    try{


    while(rs.next())
    {
    if((rs.getString("username").trim().compareTo(usr)==0)
    &&(rs.getString("password").compareTo(pwd)==0))
    {
    user=new UserTable();
    user.setId(rs.getInt(1));
    user.setUsername(rs.getString(2));
    user.setPassword(rs.getString(3));
    session.setAttribute("user",user); //把user对象存储在会话中
    validated=true; //标识为true表示验证成功通过
    }
    }
    rs.close();
    }catch(SQLException e){
    e.printStackTrace();
    }
    sqlsrvdb.closeStmt();
    sqlsrvdb.closeConn();
    }
    else{
    validated=true;
    }
    if(validated)
    {
    response.sendRedirect("main.jsp");
    }
    else{
    response.sendRedirect("error.jsp");

    }
    }
    public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,
    IOException{
    doGet(request,response);

    }




    }

    (5)编写Servlet:

    在src下创建包org.easybooks.test.servlet,在包中创建名为MainServlet的类(Servlet类)。编写MainServlet.java 代码如下:

    package org.easybooks.test.servlet;
    import java.sql.*;
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import org.easybooks.test.jdbc.OracleDBConn;
    import org.easybooks.test.model.vo.UserTable;;
    public class MainServlet extends HttpServlet {
    public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,
    IOException{
    request.setCharacterEncoding("gb2312");
    String usr=request.getParameter("username");
    String pwd=request.getParameter("password");
    boolean validated=false;
    OracleDBConn sqlsrvdb=new OracleDBConn();
    HttpSession session=request.getSession();
    UserTable user=null;
    user=(UserTable)session.getAttribute("user");
    if(user==null){
    String sql="select * from userTable";
    ResultSet rs=sqlsrvdb.executeQuery(sql);
    try{


    while(rs.next())
    {
    if((rs.getString("username").trim().compareTo(usr)==0)
    &&(rs.getString("password").compareTo(pwd)==0))
    {
    user=new UserTable();
    user.setId(rs.getInt(1));
    user.setUsername(rs.getString(2));
    user.setPassword(rs.getString(3));
    session.setAttribute("user",user); //把user对象存储在会话中
    validated=true; //标识为true表示验证成功通过
    }
    }
    rs.close();
    }catch(SQLException e){
    e.printStackTrace();
    }
    sqlsrvdb.closeStmt();
    sqlsrvdb.closeConn();
    }
    else{
    validated=true;
    }
    if(validated)
    {
    response.sendRedirect("main.jsp");
    }
    else{
    response.sendRedirect("error.jsp");

    }
    }
    public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,
    IOException{
    doGet(request,response);

    }




    }

    (6)配置Servlet:修改web.xml,代码如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>jsp_servlet_javabean_jdbc</display-name>
    <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>login.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
    <servlet-name>mainServlet</servlet-name>
    <servlet-class>org.easybooks.test.servlet.MainServlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>mainServlet</servlet-name>
    <url-pattern>/mainServlet</url-pattern>
    </servlet-mapping>

    <servlet>
    <servlet-name>selectAll</servlet-name>
    <servlet-class>org.easybooks.test.servlet.SelectAllServlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>selectAll</servlet-name>
    <url-pattern>/selectAll</url-pattern>
    </servlet-mapping>
    </web-app>

    5.部署和运行:

    项目最终形成树见下图1.4:

     

  • 相关阅读:
    Maven项目 Struts和Spring整合
    Android Binder机制
    Android屏幕适配
    AIDL初识
    Android插件化初识
    OOM解决方案
    ANR
    Android性能优化
    Android内存泄露总结
    Glide源码解析
  • 原文地址:https://www.cnblogs.com/tongying/p/14463909.html
Copyright © 2020-2023  润新知