JAVA实验用户登录代码
来源:互联网 发布:剑网3官方正太捏脸数据 编辑:程序博客网 时间:2024/06/13 23:12
实验结果部分截图:
(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(←长按可复制)获取学习资料可
- JAVA实验用户登录代码
- asp用户登录代码
- delphi 用户登录 代码
- php用户登录代码
- 用户登录代码
- 用户登录完整代码
- 南邮汇编实验二(用户登录)
- python基础练习:用户登录实验源码
- java实现用户登录
- Java 用户登录系统
- PB用户登录模块代码
- 关于asp用户登录代码
- asp.net用户登录代码
- ASP用户登录注册代码
- 用户登录界面(代码)
- android用户登录对话框代码
- android用户登录客户端代码
- smack4.2 用户登录代码
- What is the difference between Facade and Gateway design patterns?
- 对spring的理解,整理
- 九种数据类型的大小,封装类
- html5 canvas元素字体绘制
- 设计模式整理
- JAVA实验用户登录代码
- mysql存储过程详解(整理)
- Object有哪些公用方法
- OpenGL应用开发---材质
- ionic参数传递及临时存储及长期存储的思路
- 外观模式
- 存储过程详解
- Objective-C 编码规范
- 介绍几个图处理和流处理大数据平台