|
有个问题,请先看代码
1 RegistServlet.java
[php]
import java.sql.*;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class RegistServlet extends HttpServlet
{
private Connection con = null;
private ResultSet rs = null;
private RequestDispatcher dispatcher = null;
private HttpSession session = null;
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
doPost(request,response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
String userName = request.getParameter("userName");
String userPassword = request.getParameter("userPassword");
String userLevel = request.getParameter("userLevel");
String str = "SELECT username FROM userlogin WHERE username=? AND userpassword=?";
int flag = 0;
if(userName.equals("null"))
{
dispatcher = getServletContext().getRequestDispatcher("/jxd/error.html");
dispatcher.forward(request,response);
}
if(userPassword.equals(null))
{
dispatcher = getServletContext().getRequestDispatcher("/jxd/error.html");
dispatcher.forward(request,response);
}
if(userLevel.equals(null))
{
dispatcher = getServletContext().getRequestDispatcher("/jxd/error.html");
dispatcher.forward(request,response);
}
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://192.168.2.160/jxd?user=root&password=&useUnicode=true");
PreparedStatement pstmt = con.prepareStatement(str);
pstmt.setString(1,userName);
pstmt.setString(2,userPassword);
ResultSet rs = pstmt.executeQuery();
if(rs.next())
{
flag = 1;
}
rs.close();
pstmt.close();
con.close();
if(flag == 1)
{
dispatcher = getServletContext().getRequestDispatcher("/jxd/samename.jsp");
dispatcher.forward(request,response);
}
if(flag == 0)
{
insert(userName,userPassword,userLevel);
if(userLevel.equals("0"))
{
HttpServletRequest hreq = (HttpServletRequest)request;
session = hreq.getSession();
session.setAttribute("userName",userName);
dispatcher = getServletContext().getRequestDispatcher("/jxd/admi.jsp");
dispatcher.forward(request,response);
}
if(userLevel.equals("1"))
{
HttpServletRequest hreq = (HttpServletRequest)request;
session = hreq.getSession();
session.setAttribute("userName",userName);
dispatcher = getServletContext().getRequestDispatcher("/jxd/area.jsp");
dispatcher.forward(request,response);
}
if(userLevel.equals("2"))
{
HttpServletRequest hreq = (HttpServletRequest)request;
session = hreq.getSession();
session.setAttribute("userName",userName);
dispatcher = getServletContext().getRequestDispatcher("/jxd/normal.jsp");
dispatcher.forward(request,response);
}
}
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(InstantiationException e)
{
e.printStackTrace();
}
catch(IllegalAccessException e)
{
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
public void insert(String userName,String userPassword,String userLevel)
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://192.168.2.160/jxd?user=root&password=&useUnicode=true");
String str = "INSERT INTO userlogin VALUES(?,?,?,?,?,?)";
PreparedStatement pstmt = con.prepareStatement(str);
pstmt.setString(1,userName);
pstmt.setString(2,userPassword);
pstmt.setString(3,userLevel);
pstmt.setString(4,null);
pstmt.setString(5,null);
pstmt.setString(6,null);
pstmt.executeUpdate();
pstmt.close();
con.close();
return;
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(InstantiationException e)
{
e.printStackTrace();
}
catch(IllegalAccessException e)
{
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
[/php]
2 area.jsp
[php]
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<title>所管区域注册</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<p>
<%
String userName = (String)request.getAttribute("userName");
request.setAttribute("userName",userName);
%>
欢迎您, <%=userName%>区域管理员 请继续注册您管理的区域</p>
<form name="form1" method="post" action="/jxd/RegistAreaServlet">
<table width="75%" border="0">
<tr>
<td width="48%">
</td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input type="submit" name="Submit" value="注册">
</div></td>
</tr>
</table>
</form>
<p> </p>
</body>
</html>
[/php]
就是session怎么没传过去???谢谢了 |
|