本人近期正在做一个网上购书系统,用的是asp+sql+eclipse+tomca ,在做用户登录页面的时候需要将输入的数据跟数据库中的内容匹配,成功的话就跳转到登录成功页面,想知道怎么写这个checkuser的代码?还有就像管理员可以插入删除修改数据库中的数据也要连接数据库,怎么写啊?我现在连接了数据库,只能查看数据库中的内容,还不会修改?
请高手指点,小女子先谢谢了!
请高手指点,小女子先谢谢了!
解决方案 »
- 无法连接MYSQL,请指点
- 请高手赐教!无法解析类型 net.sf.hibernate.LockMode。从必需的 .class 文件间接引用了它
- 期待高手解答:jsp导出excel,流被占用
- struts中那个ActionForm为什么总是报警告
- jsp中如何将java的resultSet赋给javascript的array!急急!在线等!
- Web 2.0 编程思想:16 条法则
- 求如何查找资源未释放
- 怎样实现从数据库中读取数据然后动态的生成树型结构目录????(80分,我只有这么点分了啊)
- js 如何去除所有空格啊???
- web页面怎么解决排版问题
- tomcat6.0中运行时间长了早上起来看到几百个连接挂在那里,不能释放.
- Extjs显示一条记录
修改记录要用update语句的。
可关键是它在jsp中肯定还要写其他的语句
具体怎么跟数据库连接我就不会了
还请高手指点
<%@page import="shopping.db.*,java.sql.*" import ="java.sql.PreparedStatement" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<jsp:useBean id="getsuers" class="shopping.entity.User" scope="page">
</jsp:useBean>
<%//提交信息后,验证是否正确
String name = "";
String number = "";
String password = "";
String role = "";
String birthday = "";
String sex = "";
//if(!(session.isNew())){
name=request.getParameter("name");
if(name==null) name="";
number=request.getParameter("number");
if(number==null) number=""; password=request.getParameter("password");
if(password==null) password="";
role=request.getParameter("role");
if(role==null) role="";
birthday=request.getParameter("birthday");
if(birthday==null) birthday="";
sex=request.getParameter("sex");
if(sex==null) sex="";
//}
%>
<%
if(name.equals("")&&!number.equals("")&&!password.equals("")
&&!rple.equals("")&&!birthday.equals("")&&!sex.equals(""))
%>
<jsp:useBean id="getsuers" class="shopping.entity.User" scope="page" action="checkuser.jsp">
<jsp:setProperty name="student" property="*"/>
</jsp:useBean>
<% boolean flag=register.register(getuers);
if(flag){
response.sendRedirect("sloginsueccess.jsp");
}else {
response.sendRedirect("studentlogin.jsp");
}
%>
<html>
<head>
<title>Insert title here</title>
</head>
<body bgcolor="#ffffff">
<SCRIPT language="javascript">
alert("用户名或密码错误,请重新登录");
window.location = "studentlogin.jsp";
</SCRIPT>
<h1>
</h1>
</body>
</html>
你可以用包含也可以做个过滤器,这样用户就控制好了
if(用户验证成功){
session.setAttribute("user",user);
response.sendRedirect("sloginsueccess.jsp");
}在其他页面每次进行判断:User user=(User)session.getAttribute("user");
if(user==null){//不是登陆用户,跳转到登陆页面
response.sendRedirect("login.jsp");
}else{
//执行下面的操作
}上面的代码可以单独做个页面,其他的只要包含就可以,如:validateUser.jsp
至于管理员执行增删改的操作,只需要写相应的SQL语句,调用相应的逻辑即可,
先装个IIS吧
checkuser ——首先要会SQL语句!string sql="select * from users where name=? and pwd=?";
通过查询结果判断,如果为空则,不存在该用户无法登陆成功!不为空则登录进去!插入insert into 修改update!
不好意思各位 是用的JSP 之前打错了
登陆(login.jsp)
<%@ page contentType="text/html;charset=GB2312"%>
<html>
<head>
<title>书店租书管理系统</title>
</head>
<body bgcolor="#DCDADA">
<table border="0" width="100%" height="173">
<tr>
<td height="40">
</td>
</tr>
<tr>
<td width="100%" height="34">
<p align="center">
<font size="6"><b>书店租书管理系统<font size="4">(软件061
)</font> </b> </font>
</td>
</tr>
<tr>
<td width="100%" height="21">
<p align="center">
————软件061-13-张文琛!
</td>
</tr>
</table>
<form action="checkLogin.jsp" method="POST">
<table align="center" border="0">
<tr align="center">
<td>
<font color="red"><html:errors /> </font>
</td>
</tr>
<tr align="center">
<td>
系统登录
</td>
</tr>
<tr>
<td>
用户卡号:
<input type="text" name="cardno">
</td>
</tr>
<tr>
<td>
密 码:
<input type="password" name="passwd">
</td>
</tr>
<tr>
<td>
角 色:
<select name="role">
<option value="1">
普通管理员
</option>
<option value="2">
高级管理员
</option>
<option value="3">
普通用户
</option>
<option value="4">
高级用户
</option>
</select>
</td>
</tr>
<tr align="center">
<td>
<input type="submit" value="确定" name="submit">
</td>
</tr>
</table>
</form>
</body>
</html>
检查页面(checkLogin.jsp)
<%@ page contentType="text/html;charset=GBK"language="java" import="java.sql.*" import="java.util.*" %>
<%@ include file="conn.jsp"%>
<html>
<body>
<%
request.setCharacterEncoding("GBK");
String acardno=request.getParameter("cardno");
String apasswd=(String)request.getParameter("passwd");
String arole=(String)request.getParameter("role");
if(arole.equals("1")||arole.equals("2")){
ResultSet rs=stmt.executeQuery("select * from manager where cardno='"+acardno+"' and passwd='"+apasswd+"'");
if(rs.next()){
session.putValue("cardno",acardno);
session.putValue("passwd",apasswd);
session.putValue("role",arole);
response.sendRedirect("manager.jsp");
}
rs.close();
}
if (arole.equals("3")||arole.equals("4")){
ResultSet rs=stmt.executeQuery("select * from user where cardno='"+acardno+"' and passwd='"+apasswd+"'");
if(rs.next()){
session.putValue("cardno",acardno);
session.putValue("passwd",apasswd);
session.putValue("role",arole);
response.sendRedirect("user.jsp");
}
rs.close();
}
stmt.close();
con.close();
%>
</body>
</html>
老兄啊,不要害人,你这样会导致sql注入的。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="shopping.db.*,java.sql.*"%>
<%@page import="shopping.entity.*"%>
<%@page import="shopping.imp.*"%>
<%@page import="shopping.inter.*" %><jsp:useBean id="DB" class="shopping.db.DB"></jsp:useBean>
<jsp:useBean id="User" class="shopping.entity.User" scope="session"></jsp:useBean>
<jsp:setProperty name="User" property="*" />
<html>
<head> <TITLE>****网上购书系统</TITLE>
<META http-equiv=Content-Language content=zh-cn>
<META http-equiv=Content-Type content="text/html; charset=UTF-8">
<LINK href="images/enter.css" type=text/css rel=stylesheet>
</head> <body>
<BODY style="BACKGROUND-IMAGE: url(images/2e_bg.jpg)">
<DIV align=center>
<TABLE style="BORDER-COLLAPSE: collapse" height=576 cellPadding=0
width=990 border=0>
<TR>
<TD width=230>
<!-- leftindex.html -->
<%@include file="leftindex.jsp"%>
<!-- end leftindex.html -->
</TD> <TD width="677" valign="top"> <table width="656" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="10" colspan="3"></td>
</tr>
<tr>
<td colspan="3">
<img src="images/33e_top1.jpg" width="656" height="17">
</td>
</tr>
<tr>
<td colspan="3">
<img src="images/33e_top11.jpg" width="656" height="34">
</td>
</tr>
<tr>
<td width="2%">
<IMG height=519 src="images/33e_left.jpg" width=13 border=0>
</td>
<td width="95%" align="center" valign="top" bgcolor="#FFFFFF">
<!-- bannerindex.html -->
<%@include file="bannerindex.jsp"%>
<!-- end bannerindex.html --> <!-- main.jsp -->
<h2>
用户登录验证
</h2>
<%
String name = User.getName();
String password = User.getPassword();
boolean isLogined= false;
String sql = "select * from User where name='" + name
+ "' and password ='" + password + "'";
String message = null;ResultSet rs = null ;
Connection conn = DB.getConn();
Statement stmt = DB.createStmt(conn);
rs = stmt.executeQuery(stmt,sql) ;
if (rs.next()) {
message = "您已经登录过了!";isLogined = true ;
} else { ResultSet rs = DB.executeQuery(stmt, sql);
if (rs.next()) {
User.setLogined(true);
message = "恭喜您,登录成功!";
response.sendRedirect(request.getContextPath()
+ "/sloginsueccess.jsp"); } else {
User.setLogined(false);
message = "用户名或密码错误,请重新登陆";
response.sendRedirect(request.getContextPath()
+ "/loginfail.jsp"); }
}
%> <!-- end main.jsp -->
</td>
<td width="3%">
<img src="images/33e_right.jpg" width="21" height="519">
</td>
</tr>
<tr>
<td colspan="3">
<img src="images/33e_down.jpg" width="656" height="15">
</td>
</tr> </table>
</td>
<TD width=85>
<!-- tagindex.jsp -->
<%@include file="tagindex.jsp"%>
<!-- end tagindex.jsp -->
</TD>
</TR>
</TABLE>
</DIV> <!-- footindex.jsp -->
<%@include file="footindex.jsp"%>
<!-- end footindex.jsp -->
</body>
</html>