本人是学生菜鸟,因为最近学习网页的一些知识,要做一个网页的会员登录界面的代码,同时需要可以设置权限,连接数据库。
看了题目,有发现爱好者论坛的登录界面和设置会员权限是类似的,
我想请教各位大大,谁有代码发给小弟参考参考
就只是登录界面和设置权限那块代码
小弟感激不尽····
如果可以的话,能不能发小弟邮箱[email protected]
或者发在论坛中,给大家参考···
对各位感激不尽
看了题目,有发现爱好者论坛的登录界面和设置会员权限是类似的,
我想请教各位大大,谁有代码发给小弟参考参考
就只是登录界面和设置权限那块代码
小弟感激不尽····
如果可以的话,能不能发小弟邮箱[email protected]
或者发在论坛中,给大家参考···
对各位感激不尽
解决方案 »
- 模糊查询
- 求高手!!! java 聊天器 代码 求高手改!!!
- 多个list数据拼凑输出问题
- S2 S H整合报错,新人求教~~~~~
- 请问如何实现;在启动一个多线程时,如果服务器坏了,再重新启动服务器后;前后就没有影响多线程的操作!
- this和super
- 500大分!通过什么方法可以知道任意给定的SQL中需要绑定的参数的类型?
- struts如何与showModalDialog 方式结合使用
- 问个关于TOMCAT的小问题。。。
- 关于structs中,超连接带多个参数的问题,在线等待!
- 上传文件到MySQL数据库Blob字段 内容多了两个字节
- 数据库中的数据怎么提取出来,跟eclipse中编写的表格数据连接
GOOGLE和百度了很久还是没有找到···
有哪位大大有项目的案例么···
恳求源代码···
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%><html>
<head>
<title>登陆页面</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<link rel="stylesheet" href="./css/main.css" type="text/css" />
</head> <body> <marquee direction="left" behavior="scroll" scrollamount="5" onmouseover='this.stop()' onmouseout='this.start()' behavior=alternate
behavior="alternate" scrolldelay="150" align="top" bgolor="blue" width="100%" hspace="100">
<font size=34>学生信息管理系统</font>
</marquee> <br>
<hr>
<form action="servlet/login" name="fm1" >
<table border="1" align=center>
<tr>
<td>
用户名:
</td>
<td>
<input type="text" name="loginuser">
</td>
</tr>
<tr>
<td>
密 码:
</td>
<td>
<input type="password" name="password">
</td>
</tr>
<tr>
<td>
身 份:
</td>
<td>
<select name="sf">
<option value="1">
学生
</option>
<option value="0" selected>
教师
</option>
</select>
</td>
<tr>
<td colspan="2">
<input type="submit" name="but1" class="BtnAction" value="登陆">
<input type="reset" name="btu2" class="BtnAction" value="取消">
</td>
</tr>
</table>
</form>
</body>
</html>
后台代码import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
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 {
request.setCharacterEncoding("gbk");
String loginuser=request.getParameter("loginuser");
String type=request.getParameter("sf");
String password=request.getParameter("password");
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_xsinfo","sa","");
Statement st=con.createStatement();
Statement st2=con.createStatement();
String sql="select * from login where loginuser='"+loginuser+"' and password='"+password+"' and type='"+type+"'";
ResultSet rs1=st.executeQuery(sql);
if(rs1.next()){
if(type.equals("1")){
String url="select * from xsxx where name='"+loginuser+"'";
ResultSet rs2=st.executeQuery(url);
request.getSession().setAttribute("rs2", rs2);
response.sendRedirect("../xs_query.jsp");
}else if(type.equals("0")){
String tc="select * from xsxx";
ResultSet rs3 = st2.executeQuery(tc);
ArrayList al = new ArrayList();
while(rs3.next()){
ArrayList alTemp = new ArrayList();
alTemp.add(rs3.getString(1));
alTemp.add(rs3.getString(2));
alTemp.add(rs3.getString(5));
alTemp.add(rs3.getString(4));
alTemp.add(rs3.getString(7));
alTemp.add(rs3.getString(6));
al.add(alTemp);
}
request.getSession().setAttribute("al", al);
response.sendRedirect("../js_query.jsp");
}}else{
JOptionPane.showMessageDialog(null,"登陆失败,请重新登陆!","友情提示",JOptionPane.INFORMATION_MESSAGE);
response.sendRedirect("../login.jsp");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} /**
* 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 { response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}}
这个问题简单点说就是在点击登录按钮后调用后台,进行数据库查询,查出对应账户后再获取权限。在放入一个临时的存储地方如session,cookie。
package com.csdn.jsp.dao;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;public class Connections { public static Connection getConnection(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/xb1","root","sa");
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
}package com.csdn.jsp.dao;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;public class Connections { public static Connection getConnection(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/xb1","root","sa");
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
}
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import com.csdn.jsp.dao.Connections;public class UsersDao { public boolean validate(String username,String password){
boolean flag = false;
Connection conn = Connections.getConnection();
String sql = "select * from users where username=? and password=?";
PreparedStatement ps;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
if(rs.next()){
flag = true;
}
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
// 判断用户是否已经存在。
public boolean isExist(String username){
boolean flag = false;
Connection conn = Connections.getConnection();
String sql = "select * from users where username=?";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, username);
ResultSet rs = ps.executeQuery();
if(rs.next()){
flag = true;
}
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
//存取新用户
public void saveUser(String username, String password){
Connection conn = Connections.getConnection();
String sql = "insert into users values(?,?,?)";
try {
PreparedStatement ps = conn.prepareStatement(sql);
int id = generateID();
if(id==-1){
return;
}
ps.setInt(1, id);
ps.setString(2, username);
ps.setString(3, password);
ps.execute();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 生成下一个用户的ID。下一个用户的ID值=数据库中已有的最大ID值+1
public int generateID(){
int id = -1;
Connection conn = Connections.getConnection();
String sql = "select max(id) from users";
try {
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
if(rs.next()){
id = rs.getInt(1);
id++;
}
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return id;
}
}package com.csdn.jsp.servlet;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;import com.csdn.jsp.dao.UsersDao;public class LoginServlet extends HttpServlet { /**
* Constructor of the object.
*/
public LoginServlet() {
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;charset=gb2312");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
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 {
login(request,response);
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
} public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
UsersDao dao = new UsersDao();
if(dao.validate(request.getParameter("username"), request.getParameter("password"))){
request.getRequestDispatcher("/jsp/success.jsp").forward(request, response);
}else {
request.getRequestDispatcher("/jsp/failure.jsp").forward(request, response);
}
}
}
WebRoot/jsp/login.jsp<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page"> <!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
This is login page. <br>
<form action="servlet/LoginServlet" method="post">
用户名:<input type="text" name="username" />
密码:<input type="password" name="password" />
<input type="submit" value="提交" />
</form>
</body>
</html><?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.csdn.jsp.servlet.LoginServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>com.csdn.jsp.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/servlet/LoginServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/servlet/DispatcherServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<jsp:useBean id="user" scope="request" class="myapp.User"/>
<jsp:useBean id="md5" scope="request" class="myapp.MD5"/>
<jsp:useBean id="dateTimeFormat" scope="request" class="myapp.DateTimeFormat"/>
<%
//用户登录验证
String username = new String(request.getParameter("username").trim().getBytes("ISO-8859-1"));
String password = new String(request.getParameter("password").trim().getBytes("ISO-8859-1"));
String password1 = md5.getMD5ofStr(password); //数据加密
if (user.userExist(username)) {
response.sendRedirect("error.jsp?errorMessage=User is not valid.");
}
else if (!user.isValidUser(username, password1)) {
response.sendRedirect("error.jsp?errorMessage=Password is not valid.");
}
else {
String rights = user.userRights(username, password1); //获取用户权限
String loginIP = request.getRemoteAddr(); //获取用户IP
String loginDateTime = dateTimeFormat.getCurrentDateTime(); //获取当前日期时间
session.setAttribute("username", username); //保存用户名
session.setAttribute("rights", rights); //保存用户权限
session.setAttribute("loginIP", loginIP); //保存用户IP
session.setAttribute("loginDateTime", loginDateTime); //保存当前日期时间
user.setUserLoginInfo(loginDateTime, loginIP, username); //记录用户登录信息
user.saveUserLoginLog(username, loginDateTime); //保存用户登录日志
response.sendRedirect("main.jsp"); //打开主页面
}
%>
[email protected]
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import com.csdn.jsp.dao.Connections;public class UsersDao { public boolean validate(String username,String password){
能把这个全部发给我吗? 邮箱是 [email protected]