| 用JSP做用户身份验证的问题总结一 |
| |
发布者: 发布时间:2007-07-16 |
|
|
用户身份验证是非常常用的功能,重点在于数据库的存储.虽然简单,但是在实际操作中还是会遇到一些大大小小的问题.我通过做一个实例,来为大家作个总结.
首先,功能大致分成2个模块 1.用户信息管理 2.用户身份验证
在用户信息管理中,我们要实现添加、删除用户信息。 具体实现方法如下
<%@ page contentType="text/html;charset=GBK" language="java" %> <html> <head><title>添加用户</title></head> <body>
<form action="/insert.jsp" method="post"> <table align="center" bgcolor="#faf0e6" border="0" cellspacing="2" cellpadding="5"> <tr bgcolor="#7b68ee"> <td>用户名</td> <td><input type="text" name="usernam"></td> </tr> <tr bgcolor="#7b68ee"> <td>密码</td> <td><input type="text" name="password"></td> </tr> <tr bgcolor="#7b68ee"> <td colspan="2"><input type="submit" name="tijiao" value="添加"></td> </tr> </table> </form> </body> </html>
-------------------以上为录入用户信息部分的页面代码------------------
<%@ page contentType="text/html;charset=GBK" language="java" import="java.sql.*" import="java.io.*" %>
<html> <head> <title> insert </title> <meta http-equiv="Content-Type"content="text/html;charset=GBK"> </head> <body> <table border=1> <tr><td>姓名</td><td>密码</td></tr>
<%
String username = null; String pass = null; byte[] username1 = null; byte[] pass1 = null; String username2 =null; String pass2 =null; //--声明变量--// username = request.getParameter("usernam"); pass = request.getParameter("password"); username1 = username.getBytes("iso-8859-1"); pass1 = pass.getBytes("iso8859-1"); username2= new String(username1); pass2= new String(pass1); //---转化字符串--// Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); Connection con = java.sql.DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=hubin;user=sa"); Statement stmt = con.createStatement(); PreparedStatement pstmt = con.prepareStatement("insert into t_user(user_name,user_pass) values(?,?)"); //--连接数据库--//
pstmt.setString(1, username2); pstmt.setString(2, pass2);
pstmt.execute(); ResultSet rst = stmt.executeQuery("select * from t_user;"); //--取结果集--// while (rst.next()) { out.println("<tr>"); out.println("<td>" + rst.getString("user_name") + "</td>"); out.println("<td>" + rst.getString("user_pass") + "</td>"); } pstmt.close(); stmt.close(); con.close(); %> </table> </body> </html> -------------------以上为接收录入的信息并储存到数据库中的页面代码------------------
要注意的是,在接收录入的用户信息时,如果不转换字符编码,会出现乱码。 |
|
|
|
|
| (转载文章请保留出处:北天JAVA技术网(www.java114.com)) |
| |
| 更多精彩文章: |
| AJAX并不神秘 |
| 必须用到和不必使用ajax的地方 |
| JSP实践:使用JSP include机制改进外观 |
| 在Servlet和JSP中如何实现多线程安全? |
| Tomcat 服务器下 JSP 页面中文问题解答 |
| JSP中的pageEncoding和contentType属性 |
| |
| 最近评论: |
|
|
| 你曾悄悄的来过! |
| wow gold,wow gold,wow gold,ffxi gil max(9620) |
|
|
| 冰封的往事! |
| wow power leveling,wow gold,WoW Gold,wow gold
max(2380) |
|
|
| 飞舞的传奇! |
| 传世私服,传世私服.传奇世界私服传奇世界私服,传世私服传世私服, 传奇世界私服传奇世界私服.传奇私服传奇私服. max(5330) |
|
|
| my name is zet |
| Google |
|
|
| 回复:用JSP做用户身份验证的问题总结一 |
| sfasdad |
|
|
| Good site |
| book com diet guest mindy phentermine site [url=http://auuvd.fizwig.com/z1yq55.html]book com diet guest mindy phentermine site[/url] book burt com guest pharmacy phentermine site [url=http://auuvd.fizwig.com/buy-overnight-phentermine.html]book burt com guest pharmacy phentermine site[/url] cheap phentermine pill [url=http://auuvd.fizwig.com/mvnub.html]cheap phentermine pill[/url] |
|
|
| Good site |
| buy phentermine no prescription [url=http://auuvd.fizwig.com/uu7ho2x.html]buy phentermine no prescription[/url] cheap phentermine order by [url=http://auuvd.fizwig.com/76bjjk2.html]cheap phentermine order by[/url] pre employment drug screen taking phentermine with [url=http://auuvd.fizwig.com/yd1f75d.html]pre employment drug screen taking phentermine with[/url] |
|
|
| |
| 免责声明:该文章由网友发表,如果对您造成侵权,请联系站长。 |