帮我看一个servletjsp的问题

xinnian25 2012-03-08 03:18:29

登录的servlet代码

public class Login extends HttpServlet{

public void init(ServletConfig config) throws ServletException{
super.init(config);
}

public void service(HttpServletRequest req,HttpServletResponse resp) throws IOException{

HttpSession session=req.getSession(true);
PrintWriter out = resp.getWriter();
String username=req.getParameter("username");
String pwd=req.getParameter("pwd");
String sql="select * from userinfo where username=? and pwd=?";

Connection conn=db.getConnection();

try{
PreparedStatement pstmt=conn.prepareStatement(sql);

pstmt.setString(1,username);
pstmt.setString(2,pwd);

ResultSet rs=pstmt.executeQuery();
Boolean m=rs.next();
if(m==true){
session.setAttribute(username, rs.getString("username"));
session.setAttribute(pwd,rs.getString("pwd"));

resp.sendRedirect("index.jsp");
}else{
out.println("<SCRIPT LANGUAGE=javascript>");
out.println("alert('用户名或密码错误!');");
out.println("window.location.href='default.jsp'; ");
out.println("</script>");
}
pstmt.close();
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}

}
}


查询数据的servlet
public class gl extends HttpServlet{

/**
*
*/
private static final long serialVersionUID = 1L;

public void init(ServletConfig config) throws ServletException{
super.init(config);
}

public void service(HttpServletRequest req,HttpServletResponse resp) throws IOException{

HttpSession session=req.getSession(true);
PrintWriter out = resp.getWriter();
String username=null;
String lastip=null;
String sql="select username,lastip from log";

Connection conn=db.getConnection();

try{
PreparedStatement pstmt=conn.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery(sql);
Boolean m=rs.next();
if(m==true){
session.setAttribute(username,rs.getString("username"));
session.setAttribute(lastip,rs.getString("lastip"));
System.out.println(username);
}else{
out.println("<SCRIPT LANGUAGE=javascript>");
out.println("alert('用户名或密码错误!');");
out.println("window.location.href='default.jsp'; ");
out.println("</script>");
}
pstmt.close();
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}

}
}


登录的jsp页面

<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'default.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 my JSP page. <br>
<form action="login" name="form1" method="post">
<tr>
<td>用户</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="pwd"></td>
</tr>
<tr>
<td height="45" colspan="2">
<p align="center">
<input type="submit" value="登 陆">
  
<input type="reset" value="取 消">
</td>
</tr>
</form>
</body>
</html>



登录成功后转向的页面

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*"%>
<%@ page import="org.gl.*"%>
<%@ page import="java.util.Date"%>
<jsp:useBean id="db" class="org.gl.db" scope="page"/>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<%
String username=(String)session.getAttribute("username");
String lastip=(String)session.getAttribute("lastip");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.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>XXXXXXXXXXXXXXXX This is my JSP page. <br>

<form action="gl" method="post">

<table border="1">

<tr><td><%=username%></td><td><%=lastip%></td></tr>

</table>
</form>
</body>
</html>


web.xml
	
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>org.gl.Login</servlet-class>
</servlet>
<servlet>
<servlet-name>gl</servlet-name>
<servlet-class>org.gl.gl</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>gl</servlet-name>
<url-pattern>/gl</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>




我要实现的是一个简单的登录进入,然后转到index页面,显示出从数据库里查询出来的数据

但不知道为什么为null,传不到值,求指教

...全文
154 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
snowsowhite 2012-03-08
  • 打赏
  • 举报
回复
String username=req.getParameter("username");
String pwd=req.getParameter("pwd");

session.setAttribute(username, rs.getString("username"));
session.setAttribute(pwd,rs.getString("pwd"));
username和pwd都是动态的,前台要怎么获取,应改为
session.setAttribute("username", rs.getString("username"));
session.setAttribute("pwd",rs.getString("pwd"));

下面也一样,你都把username和lastip设成空了,session的key值没了,前台要怎么获取
String username=null;
String lastip=null;
session.setAttribute(username,rs.getString("username"));
session.setAttribute(lastip,rs.getString("lastip"));
应改为
session.setAttribute("username", rs.getString("username"));
session.setAttribute("lastip",rs.getString("pwd"));
或者:
String username="username";
String lastip="lastip";
fedori 2012-03-08
  • 打赏
  • 举报
回复
index.jsp 必须和登录的jsp页面同一个文件夹。
昨日凡阳 2012-03-08
  • 打赏
  • 举报
回复
在index.jsp,你何时提交表单,访问

查询得servlet?
xinnian25 2012-03-08
  • 打赏
  • 举报
回复
我登录已经实现了,就是可以登录进入,但是index页面的只显示为null

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧