博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Servlet访问postgresql数据库并提取数据显示在前端jsp页面
阅读量:6510 次
发布时间:2019-06-24

本文共 7429 字,大约阅读时间需要 24 分钟。

hot3.png

1     结构

174342_ok9U_2550349.png

2     LoginTestServlet.java

package com.test;        import java.io.IOException;  import java.sql.Connection;import java.sql.DriverManager;  import java.sql.ResultSet;        import java.sql.Statement;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 javax.servlet.http.HttpSession;                   /**      * Servlet implementation class LoginTestServlet      */      @WebServlet("/LoginTestServlet")      public class LoginTestServlet extends HttpServlet {          private static final long serialVersionUID = 1L;                       /**          * @see HttpServlet#HttpServlet()          */          public LoginTestServlet() {              super();                        }                /**          * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)          */          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {              doPost(request, response);          }                /**          * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)          */          protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {              response.setContentType("text/html;charset=gb2312");              request.setCharacterEncoding("gb2312");                            String result = "";                        String username = request.getParameter("username");              String psw = request.getParameter("password");                            if(username == "" || username == null || username.length()>20){                  try {                      result = "请输入用户名(不能超过20个字符)";                      request.setAttribute("message", result);                      response.sendRedirect("login.jsp");                      return;                  } catch (Exception e) {                      e.printStackTrace();                  }              }                            if(psw == "" || psw == null || psw.length() > 20){                  try {                      result = "请输入密码(不能超过20个字符)";                      request.setAttribute("message", result);                      response.sendRedirect("login.jsp");                      return;                  } catch (Exception e) {                      e.printStackTrace();                  }              }                            //登记JDBC驱动程序              try {                  Class.forName("org.postgresql.Driver");              } catch (Exception e) {                  System.out.print("Class Not Found Exception");              }                            //链接URL              String url = "jdbc:postgresql://localhost:5432/mydb";              Connection conn = null;              Statement stmt = null;              ResultSet rs = null;                            try {                  conn = (Connection) DriverManager.getConnection(url,"postgres","postgres");                  stmt = (Statement) conn.createStatement();                                    String sql = "select * from userInfo where username='"+username+"' and userpsw= '"+psw+"'";                  rs = stmt.executeQuery(sql);              } catch (Exception e) {                  e.printStackTrace();              }                        HttpSession session = request.getSession();          session.setAttribute("username", username);                        try {              if(rs.next()){                  session.setAttribute("age",rs.getString("age")) ;                  session.setAttribute("sex",rs.getString("sex")) ;                  session.setAttribute("weight",rs.getString("weight")) ;                  response.sendRedirect("success.jsp") ;                  return;              }else{                  session.setAttribute("message", "用户名或密码不匹配。");                  response.sendRedirect("fail.jsp") ;                  return;              }                        } catch (Exception e) {              e.printStackTrace();          }          }            }

3    login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
User Login    
          
用户登录          
              
                                        用户名:                                                
                                                                                密  码:                                                
                                                                                                          
                                                                      
                                                                
                  
                 function validateLogin(){                   var sUserName = document.frmLogin.username.value;                   var sPassword = document.frmLogin.psssword.value;                  if (sUserName ==""){                   alert("请输入用户名!");                   return false ;                  }                                    if (sPassword ==""){                   alert("请输入密码!");                   return false ;                  }          }        

4        success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
Insert title here<%     String userName = (String)session.getAttribute("username") ;     String age = (String)session.getAttribute("age") ;     String weight = (String)session.getAttribute("weight") ;     String sex = (String)session.getAttribute("sex") ;     System.out.println("性别:A" + sex +"A")  ;   /*   if (sex.trim().equals("M")) {      sex ="男" ;     }else{      sex = "女" ;     }   */  %>    
     <%=userName %>     欢迎您,登陆成功!
     
登陆用户信息:     
              姓名:          <%=userName %>                      年龄:          <%=age %>                      体重:          <%=weight %> kg                     性别:          <%=sex %>                  
返回      

5      fail.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
Insert title here<%          String userName = (String) session.getAttribute("username");          String msg = (String) session.getAttribute("message");      %>      
          <%=userName%>          对不起,登陆失败!
 
原因:           <%=msg%>          
 
 5秒后将返回登陆界面。              <%          response.setHeader("Refresh", "5;URL=/ServletDemo01/login.jsp");      %>

6.      数据库表

CREATE TABLE userinfo(  username character varying NOT NULL,  userpsw character varying NOT NULL,  age integer,  sex character(1),  weight integer,  CONSTRAINT userinfo_pkey PRIMARY KEY (username))

7.web.xml

  
ServletDemo01
  
    
index.html
    
index.htm
    
index.jsp
    
default.html
    
default.htm
    
default.jsp
  
      
          
LoginTestServlet
          
com.test.LoginTestServlet
      
            
          
LoginTestServlet
          
/loginTestServlet
      
  

转载于:https://my.oschina.net/chumingcheng/blog/609973

你可能感兴趣的文章
多核编程的四层境界
查看>>
Windows Phone 实用开发技巧(11):让StackPanel中的控件靠右对齐
查看>>
小记如何修改xen模块
查看>>
实时游戏对战引擎Photon
查看>>
C语言位操作控件属性
查看>>
nginx的安装及基本配置,及多个域名服务
查看>>
Servlet访问postgresql数据库并提取数据显示在前端jsp页面
查看>>
不改一行代码定位线上性能问题
查看>>
定义运算符
查看>>
git管理
查看>>
idea演示
查看>>
告别暗黄皮肤变水嫩皮肤的8个小习惯
查看>>
加强Eclipse代码自动提示的方法
查看>>
【HM】第4课:MySQL入门
查看>>
GNS3-地址重叠环境中部署IPsec
查看>>
exchange online 用户疑问之许可证和用户数据归档
查看>>
QImage Mat IplImage 之间的相互转换
查看>>
lsof命令详解
查看>>
使用eclipse与android studio 在开发自定义控件时的区别
查看>>
我的友情链接
查看>>