比如,1.jsp,2.jsp错在那里?
1.jsp:<%@ page contentType="text/html; charset=gb2312" %>
<html><form name = "1" action="2.jsp" method = post>
<%
HttpSession session;
session.setAttribute("aa","1");%>
<table><tr>
<td align=center><input type="text" name="text" value=""></td>
</tr>
<tr><td align = center><input type="submit" name="submit" ></tr>
</table>
</html>
--------------------------------------------------------------------------
2.jsp<%@ page contentType="text/html; charset=gb2312" %>
<html>
<% HttpSession session = request.getSession();
String s = session.getId();
String a = (String)(session.getAttribute("aa")); %> <table><tr>
<td align=center><input type="text" name="text" value = s></td>
</tr>
</table>
</html>
1.jsp:<%@ page contentType="text/html; charset=gb2312" %>
<html><form name = "1" action="2.jsp" method = post>
<%
HttpSession session;
session.setAttribute("aa","1");%>
<table><tr>
<td align=center><input type="text" name="text" value=""></td>
</tr>
<tr><td align = center><input type="submit" name="submit" ></tr>
</table>
</html>
--------------------------------------------------------------------------
2.jsp<%@ page contentType="text/html; charset=gb2312" %>
<html>
<% HttpSession session = request.getSession();
String s = session.getId();
String a = (String)(session.getAttribute("aa")); %> <table><tr>
<td align=center><input type="text" name="text" value = s></td>
</tr>
</table>
</html>
不好意思。
代码:
1.login.jsp
<HTML>
<HEAD>
</HEAD>
<BODY>
<center>
<form name='form1' method='post' action='/servlet/Loginservlet'>
<br>
<table width="300" border="1" cellspacing="0" cellpadding="0" height="22"
bordercolor="#003333" align=center>
<tr>
<td bgcolor="#003333" height="18">
<div align="center"><font color="#FFFFFF" class="white">登录</font></div>
</td>
</tr>
</table> <table width="300" border="1" cellspacing="0" cellpadding="0"
bordercolor="#003333" height="129">
<tr>
<td>
<div align="center">
<table width="159" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="67" height="33">
<div align="center" class="item" nowrap>登入用户名:</div>
</td>
<td width="92" height="33">
<input type="text" id="username" name="username" size="12"
style="FONT-SIZE: 9pt">
</td>
</tr>
<tr>
<td width="67">
<div align="center" class="item" nowrap>密<font
color="#FFFFFF">....</font>码:
</div>
</td>
<td width="92">
<input type="password" name="password" size="12"
style="FONT-SIZE: 9pt">
</td>
</tr>
</table>
<table width="167" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<div align="center"><br>
<input type="submit" name="Submit" value="登入系统" >
</div>
</td>
</tr>
<tr><td> </td></tr>
</table>
<br>
</div>
</td>
</tr>
</form>
</center>
</BODY>
</HTML>
<SCRIPT LANGUAGE=javascript>
<!--
document.form1.username.focus();
-->
</SCRIPT>2.Loginservlet.java/*引入包文件*/
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.*;public class Loginservlet extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,java.io.IOException
{
String sDBDriver="oracle.jdbc.driver.OracleDriver";
Connection conn=null;
ResultSet rs=null;
Statement stmt=null;
PrintWriter out;
String strUserName="",strPassword="",strPrompt="",strSQL="",strTemp="";
boolean fAuthorized=false;
int i=2;
HttpSession session=req.getSession();
out = resp.getWriter();
try{
Class.forName(sDBDriver);
}catch(java.lang.ClassNotFoundException e){
System.err.println("dbbean(): "+e.getMessage());
}
try{
conn=DriverManager.getConnection
("jdbc:oracle:thin:@lmz:1521:labora","scott","tiger");
stmt=conn.createStatement();
}catch(SQLException e){}
/**get parameter**/
strUserName=req.getParameter("username");
strPassword=req.getParameter("password");
//instance session variable:
session.putValue("login_name","");
session.putValue("user_name",""); //Authenticate()
strSQL = "select login_name,user_pwd from sys_user where
login_name='"+strUserName+"'";
try{
rs=stmt.executeQuery(strSQL);
if(rs.next()){
fAuthorized=true;
rs.close();
}else{
fAuthorized=false;
rs.close();
}
conn.close();
stmt.close();
}catch(SQLException e){
out.println("数据库错误!"+e.getMessage()+"\n");
out.println(strUserName);
System.err.println("executeQuery(): "+e.getMessage());
} if(fAuthorized){
strTemp=(String)session.getValue("login_name");
if (strTemp.compareTo("super")!=0){
strSQL = "insert into sys_log
(user_name,prg_id,optype,primary,value,computer,ip) values('"+
strUserName+"','00000','登录成功','登录名','"+
session.getValue("user_name")+"','"+req.getRemoteHost()+
"','"+req.getRemoteAddr()+"')"; try{
int iReturn=stmt.executeUpdate(strSQL);
}catch(SQLException e){}
} if (((String)session.getValue("login_name")).compareTo("super")
==0)
{
//resp.sendRedirect("/jsp/xxx.html");
}else{//登录失败!
strPrompt="登录失败,请重新登录。";
strSQL = "insert into sys_log
(user_name,prg_id,optype,primary,value,computer,ip) values('"
+strUserName+"','00000','登录失败','登录名','"+
(String)session.getValue("user_name")+"','"+
req.getRemoteHost()+"','"+req.getRemoteAddr()+"')"; try{stmt.executeUpdate(strSQL);}
catch(SQLException e){}
resp.sendRedirect("/login.jsp");
}
}
session直接用就可以了
还有我比较喜欢用session.getValue("XXX"),session.putValue("XXX")百试百灵
一般用session.getValue(),session.putValue("",)都行!