大家帮帮我啊,不会忘记你们大恩大德的,顶也有分,而且还可以加分。

shuibianle1 2004-10-19 10:37:14
运行一个注册摸块出错,只贴出了相关的代码(一个JSP,一个JAVABEAN)。

JSP代码如下
<%@ page contentType="text/html;charset=gb2312" pageEncoding="gb2312" %>
<html>
<head>
<title>Shopping123</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="styles/shoppingstyle.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0">
<jsp:useBean id="checklogin" scope="page" class="abc.checkLogin" />
<%
boolean login = checklogin.check(request,response);
%>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr bgcolor="#990000">
<td height="80" colspan="5"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="120"> </td>
<td class="caption">Shopping123</td>
<td width="200"> </td>
</tr>
</table></td>
</tr>
<tr>
<td width="200" align="center" valign="top"><table width="100%" height="20" border="0" cellpadding="0" cellspacing="0">
<tr>
<td> </td>
</tr>
</table>
<%
if(!login){
%>
<table width="90%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<form name="form1" method="post" action="/servlet/login">
<tr align="center" bgcolor="#CCCCCC">
<td height="30" colspan="2" class="deepred">卖场入口</td>
</tr>
<tr>
<td width="50%" height="24" align="center" bgcolor="#FFFFFF">会员</td>
<td align="center" bgcolor="#FFFFFF"><input name="username" type="text" id="username" size="10"></td>
</tr>
<tr>
<td height="24" align="center" bgcolor="#FFFFFF">密码</td>
<td align="center" bgcolor="#FFFFFF"><input name="password" type="text" id="password" size="10"></td>
</tr>
<tr>
<td height="24" align="center" bgcolor="#FFFFFF"><a href="reg.jsp" target="_blank" class="red">注册</a></td>
<td align="center" bgcolor="#FFFFFF"><input type="submit" name="Submit" value="进入"></td>
</tr>
</form>
</table>
<%
}
else
{
out.println("您好," + checklogin.getUserName() + "!");
}
%>
</td>
<td width="1" valign="top" bgcolor="#CCCCCC"></td>
<td width="400"> </td>
<td width="1" valign="top" bgcolor="#CCCCCC"></td>
<td width="200"> </td>
</tr>
<tr align="center" bgcolor="#990000">
<td height="60" colspan="5" class="white">copyright© 2003 Shopping123</td>
</tr>
</table>
</body>
</html>

JAVABEAN如下:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

//class checkLogin
public class checkLogin
{
public String username = "";

public boolean check(HttpServletRequest req,HttpServletResponse res)
throws IOException,ServletException
{
String cookieName = "username";
Cookie[] myCookies = req.getCookies();
this.username = this.getCookieValue(myCookies,cookieName,"not found");
PrintWriter out = res.getWriter();
if(this.username != null)
{
//out.println("早上好," + this.username + "!");
return true;
}else{
out.println("登陆失败!");
return false;
}

}

public String getUserName()
{
return this.username;
}

public static String getCookieValue(Cookie[] cookies,String cookieName,String defaultValue)
{
for(int i=0;i<cookies.length;i++) {
Cookie cookie = cookies[i];
if (cookieName.equals(cookie.getName()))
return(cookie.getValue());
}
return(defaultValue);
}
}
...全文
164 点赞 收藏 26
写回复
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
xyxu1982 2004-10-21
JAVABEAN打个包就没问题了
package abc;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

//class checkLogin
public class checkLogin
{
public String username = "";

public boolean check(HttpServletRequest req,HttpServletResponse res)
throws IOException,ServletException
{
String cookieName = "username";
Cookie[] myCookies = req.getCookies();
if (myCookies!=null) {
username = getCookieValue(myCookies,cookieName);
PrintWriter out = res.getWriter();
if(username != null)
{
//out.println("早上好," + username + "!");
return true;
}else{
out.println("登陆失败!");
return false;
}
}
return false;
}

public String getUserName()
{
return username;
}

public static String getCookieValue(Cookie[] cookies,String cookieName)
{
for(int i=0;i<cookies.length;i++) {
Cookie cookie = cookies[i];
if (cookieName.equals(cookie.getName()))
return(cookie.getValue());
}
return null;
}
}
回复
oldbig615 2004-10-20
路过,顶一吓
回复
guojiafuzhuxi 2004-10-20
很晚了,明天研究一下!UP
回复
紫翎观星 2004-10-20
╭═══╮ ╭═══╮ ╭══════╮   
╰╮ ╭╯ ╰╮ ╭╯ ╰╮ ╭══╮╰╮  
 ║秋║  ║秋║   ║秋║  ╰╮╰╮ 
 ║天║  ║天║   ║天║   ║ ║ 
 ║的║  ║的║   ║的║   ║ ║ 
 ║童║  ║童║   ║童║  ╭╯╭╯ 
 ║话║  ║话║   ║话╰══╯╭╯  
 ║ ║  ║ ║   ║ ╭═══╯   
 ║专║  ║专║   ║专║       
 ║用║  ║用║   ║用║       
 ║ ║   ║ ║   ║ ║       
 ╰╮╰╮ ╭╯╭╯   ║ ║       
  ╰╮╰═╯╭╯   ╭╯ ╰╮      
   ╰═══╯    ╰═══╯
回复
禽兽v5 2004-10-20
表说得那么凄惨。
回复
herman-chen 2004-10-20
帮顶
回复
victorylj 2004-10-20
java.lang.NullPointerException表示空指针异常,这个可能是提交的参数个数或变量名不匹配造成的
回复
易点互联 2004-10-20
楼主我把你的代码放到我的机子上试了下,按照cm4ever(小P) 的方法改了就可以了,他的代码如下
public static String getCookieValue(Cookie[] cookies,String cookieName,String defaultValue)
{
if ( cookies != null )
{
for(int i=0;i<cookies.length;i++) {
Cookie cookie = cookies[i];
if (cookieName.equals(cookie.getName()))
return(cookie.getValue());
}
}

return(defaultValue);
}

但是按照你的逻辑,还要改check()如下
public boolean check(HttpServletRequest req,HttpServletResponse res)
throws IOException,ServletException
{
String cookieName = "username";
Cookie[] myCookies = req.getCookies();
this.username =getCookieValue(myCookies,cookieName,"");
PrintWriter out = res.getWriter();
if(this.username != null&&this.username.length()!=0)
{
//out.println("早上好," + this.username + "!");
return true;
}else{
out.println("登陆失败!");
return false;
}
}
要不然就不会出现登陆窗口了.你也可以改为其他的判断
回复
qiyao 2004-10-20
<--------------高手看过来! 能不能搞定这个?
http://community.csdn.net/Expert/topic/3472/3472337.xml?temp=.1701624
回复
sxfzr 2004-10-20
没有什么好的意见,一点一点测不错,顶一下!
回复
易点互联 2004-10-20
按照 cm4ever(小P) 的方法,判断下是否为null就解决了
回复
qiyao 2004-10-20
<--------------高手看过来! 能不能搞定这个?
http://community.csdn.net/Expert/topic/3472/3472337.xml?temp=.1701624
回复
googolplex 2004-10-20
楼上有理
回复
kingmaxno1 2004-10-20
楼主,这些问题根本不用争来争去的,你不是知道他是怎么写的吗,
你把它测一下,不就完了吗,还用在这郁闷啊,
最简单的办法,你在它写的bean里加一个System.out.println("觉的有问题的变量");
这样它就可以显示在服务器的dos窗口里了,
大家总在这里觉的这,觉的那的,不如果动手做一下
回复
shaopin 2004-10-20
1 第一次访问,cookies是空的,直接引用cookies.length所以出错
2 cookieName.equals(cookie.getName())永远不会成立,程序永远不会得到正确结果
回复
shuibianle1 2004-10-20
那个NullPointerException解决了,现在有出现新的错误大家再帮我看看。谢谢
type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP

No Java compiler was found to compile the generated source for the JSP.
This can usually be solved by copying manually $JAVA_HOME/lib/tools.jar from the JDK
to the common/lib directory of the Tomcat server, followed by a Tomcat restart.
If using an alternate Java compiler, please check its installation and access path.

org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:128)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:307)
org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:416)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:456)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:440)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:555)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:300)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:294)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)

note The full stack trace of the root cause is available in the Tomcat logs.
回复
wwwer1 2004-10-20
java.lang.NullPointerException
空指针错误。
搜索关键字NullPointerException
好多经典回复的
回复
jackkui 2004-10-20
Cookie[] myCookies = req.getCookies();
检查一下获得的是否为空,然后再对他进行调用否则会出现
运行时异常,就像你的空指针异常一样。
回复
wandou999 2004-10-20
uuuuuuuuuuuuup
回复
sysmanager 2004-10-20
只能帮顶了.
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告