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

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);
}
}
...全文
197 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
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~~ 2004-10-20
  • 打赏
  • 举报
回复
帮顶
香芋元宇宙 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
  • 打赏
  • 举报
回复
只能帮顶了.
加载更多回复(6)

81,092

社区成员

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

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