JAVA实验用户登录代码
2016-12-16 01:25
429 查看
实验结果部分截图:
(1)错误的用户名密码
(2)错误提示页
(3)正确的用户名密码
(4)成功页面
实验代码:
Login部分:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Login extends HttpServlet {
/**
* Constructor of the object.
*/
public Login() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get. *
* @param request the request send by the client to the server
* @param response the response send by the server to the client * @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("gb2312");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD>");
out.println("<TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print("<form id=\"form1\" name=\"form1\" method=\"post\" action=\"./loginuser\">");
out.print("<p style=\"text-align: center\">");
out.println("<label for=\"ID_USER\">用户名:</label>"); out.println("<input type=\"text\" name=\"ID_USER\" id=\"ID_USER\" />"); out.println("</p>"); out.println("<p style=\"text-align: center\">");
out.println("<label for=\"ID_PASS\">密 码:</label>");
out.println("<input type=\"text\" name=\"ID_PASS\" id=\"ID_PASS\" />");
out.println("</p>");
out.println("<p style=\"text-align: center\">");
out.println("<input type=\"submit\" name=\"ID_SUBMIT\" id=\"ID_SUBMIT\" value=\"登录\" />");
out.println(" ");
out.println("<input type=\"reset\" name=\"ID_RESET\" id=\"ID_RESET\" value=\"重置\" />");
out.println("</p>");
out.println("</form>");
out.println("</body>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
Loginuser部分:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Loginuser extends HttpServlet {
/**
* Constructor of the object.
*/
public Loginuser() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
User newUser = new User();
newUser.userName = request.getParameter("ID_USER");
newUser.passWord = request.getParameter("ID_PASS");
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println("<p style=\"text-align: center\">");
if (User.k1(newUser))
out.println("登陆成功!");
else
out.println("登陆失败!");
out.println("</P>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
} 4.01
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
User部分:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class User {
public String userName;
public String passWord;
static boolean h =false;
private static String DBDRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";//数据库URL
private static String DBURL="jdbc:sqlserver://localhost:1433;DatabaseName=student"; public static boolean k1(User a){
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
try{
Class.forName(DBDRIVER);//装载驱动程序 //建立与数据库的连接 con=DriverManager.getConnection(DBURL,"sa","{{123456:0}}"); }catch(ClassNotFoundException e){ System.err.println("ClassNotFoundException:"+e.getMessage()); System.exit(1); }catch(SQLException e){ System.err.println("SQLException:"+e.getMessage());
System.exit(1); } try{
stmt=con.createStatement();
rs=stmt.executeQuery("Select * From users '"+a.userName+"'");
while (rs!=null&&rs.next())
{
System.out.println(rs.getString("password")); if(a.passWord.equals(rs.getString("password"))) return true;
else
return false;
}
}catch(SQLException e){
System.err.println("SQLException: "+e.getMessage()); }finally{
if(rs!=null){
try{
rs.close();
}catch(SQLException e){}
}
if(stmt!=null){
try{
stmt.close();
}catch (SQLException e){}
}
if(con!=null){
try{
con.close();
}catch(SQLException e){}
}
}
return h;
}
} where username =
学习java和学习其他知识一样,都会遇到困难与瓶颈期,关键在于不放弃。学习的动力在于,你为什么学,是跟风?还是自己心里真的喜欢?如果轻言放弃,那么就根本不是喜欢。真正从心里认可的事情,是无论如何都会想办法去做的。一起学习java的伙伴加java思维交流群:175161984(←长按可复制)获取学习资料可
相关文章推荐
- BDF2-JAVA代码以及view页面获取当前登录用户的信息
- 【用户授权设计】java第三方登录(微博,QQ)详细代码
- JavaWeb实现用户登录注册功能实例代码(基于Servlet+JSP+JavaBean模式)
- Eclipse 连接 SQL Server 2008数据库,运行JAVA代码时出现:‘sa’用户登录失败
- 使用java代码判断用户使用哪种方式登录的。
- Java用户登录验证代码
- java编程基础之模仿用户登录代码分享
- java语言图形用户登录界面代码
- JAVA Script 脚本代码 用户密码 验证
- [导入]用户登录及验证用户是否登录的代码
- 用vbs确定用户的登录名的代码
- java代码获取数据库连接并操作数据库用户
- delphi 用户登录 代码
- 登录口令加密函数、用户登录主Form实现方法和具体代码
- 地磅称量系统之(27~29)实现主界面显示用户的登录信息和日期时间星期的代码
- 如何用存储过程及SQL代码创建登录、用户和角色
- 常用代码-用户登录参考代码
- SilverLight学习笔记--如何在xaml文件中操作用户在后台代码定义的类(2)--示例篇:创建一个登录控件(原创)(转载本文请注明出处)
- ASP .NET登录界面用户验证码代码
- Oracle Sys用户用默认密码change_on_install 无法登录的问题(错误代码:ORA-28009)