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("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");

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(←长按可复制)获取学习资料可

0 0
原创粉丝点击