登陆页面源代码

来源:互联网 发布:md4算法 编辑:程序博客网 时间:2024/04/29 06:09

 CheckServlet.class

package Login; import java.io.*; import java.net.*; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.*; import javax.servlet.http.*; public class CheckServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); String name = request.getParameter("uname"); String pword = request.getParameter("pword"); String path = request.getContextPath(); if (login(name, pword)) { path = path + "/success.jsp"; } else { path = path + "/index.jsp"; } response.sendRedirect(path); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } public String getServletInfo() { return "Short description"; } private boolean login(String uname, String pword) { boolean flag = false; String sql = "select count(*) from d_xt_yh where yh_mc='" + uname + "' and yh_mm='" + pword + "'"; try { ResultSet rs = Dbutil.executeQuery(sql); while (rs.next()) { if (rs.getInt(1) > 0) flag = true; } } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return flag; } }

数据库连接类文件

package Login; import java.sql.*; public class Dbutil { static String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; static String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=xmglxt"; static String user = "sa"; static String pwd = "sa"; public static Connection getConn() throws ClassNotFoundException, SQLException { Connection conn = null; Class.forName(driver); conn = DriverManager.getConnection(url, user, pwd); return conn; } public static ResultSet executeQuery(String sql) throws SQLException, ClassNotFoundException { Connection conn = Dbutil.getConn(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); // Dbutil.close(conn, stmt,rs); return rs; } public static void close(Connection conn, Statement stmt, ResultSet rs) throws SQLException { rs.close(); stmt.close(); conn.close(); } }

 

JSP文件

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>登陆</title>
    </head>
    <body>
<center><br><br><br>

<form action="Login/CheckServlet" method="post">
<table width="259" border="1" cellpadding="0" cellspacing="0" bordercolor="#0099FF">
<tr align="center">
    <td height="20" colspan="2"><span class="style1">登陆</span></td>
</tr>
<tr>
    <td width="50" height="20">用户名</td>
    <td width="161" align="left"><input name="uname" type="text" id="uname" size="19"></td>
</tr>
<tr>
    <td height="20">密码</td>
    <td align="left"><input name="pword" type="password" id="pword" size="20"></td>
</tr>
<tr align="center">
    <td colspan="2"><input type="submit" name="Submit" value="提交">&nbsp;&nbsp;
      <input type="reset" name="Submit" value="重置"></td>
</tr>
</table>
</form>

</center>
    </body>
</html>

原创粉丝点击