JavaWeb求教大神,为什么我这个会报错??

nichilemaworld 2014-11-13 04:59:09
本人刚开始学JavaWeb,这是跟着视频做的第一个程序,我这个为什么运行不出来,tomcat已经启动,求大家指教,不胜感激!
以下是代码和报错:
这是LoginServlet
package bbk;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


String username = request.getParameter("username");
String password = request.getParameter("password");

boolean flag = false;
if(username.equals("lc") && password.equals("123"))
{
flag = true;
}

request.setAttribute("isSuccess", flag);
request.getRequestDispatcher("/info.jsp").forward(request, response);
}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doGet(request,response);
}

}

这是XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>bbk.LoginServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>

</web-app>

这是login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'login.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>
<form action="LoginServlet" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit">
</form>
</body>
</html>

这是info.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'info.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>
<%
Boolean isSuccess = (Boolean)request.getAttribute("isSuccess");
if(isSuccess){
out.print("欢迎光临");
}else{
out.print("登陆失败");
}
%>
</body>
</html>
----------------------------这是报错内容----------------------------------
HTTP Status 500 -

type Exception report

message

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

exception

java.lang.NullPointerException
bbk.LoginServlet.doGet(LoginServlet.java:22)
javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.14 logs.

Apache Tomcat/8.0.14
...全文
405 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
woeser 2014-11-14
  • 打赏
  • 举报
回复
whdrs 2014-11-14
  • 打赏
  • 举报
回复
nichilemaworld 2014-11-13
  • 打赏
  • 举报
回复
我自己把问题解决了!哈哈! 我相信楼上各位大神做梦都没想到我这个新手白痴是怎么犯的错我应该先在浏览器运行login.jsp的,而不是上来直接运行servlet,作为个新手真是太白痴了,还是感谢楼上各位今晚的付出,谢谢你们了!
nichilemaworld 2014-11-13
  • 打赏
  • 举报
回复
引用 19 楼 magi1201 的回复:
就加到你后台的servlet里面 结果会在tomcat控制台打印出来,如果是eclipse集成的tomcat,那就在eclipse的控制台打印出来
看到了,tomcat控制台显示这两个值都是null,这是怎么造成的?问题是不是出在登陆界面上??应该怎么修改下??
姜小白- 2014-11-13
  • 打赏
  • 举报
回复
就加到你后台的servlet里面 结果会在tomcat控制台打印出来,如果是eclipse集成的tomcat,那就在eclipse的控制台打印出来
nichilemaworld 2014-11-13
  • 打赏
  • 举报
回复
引用 17 楼 magi1201 的回复:
String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println("username=" + username); System.out.println("password=" + password ); 看看需要的username和password有没有传递到后台
我真是太笨了,弱弱的问句,这句话加在哪里呢??结果会在哪里显示出来??
姜小白- 2014-11-13
  • 打赏
  • 举报
回复
String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println("username=" + username); System.out.println("password=" + password ); 看看需要的username和password有没有传递到后台
nichilemaworld 2014-11-13
  • 打赏
  • 举报
回复
引用 14 楼 magi1201 的回复:
if ((null != username) && (null != password) && "lc".equals(username.trim()) && "123".equals(password.trim())) 进行判断 防止传进来的username 或者 password 前后有空字符的影响
按您说的修改后依然还是直接显示“登录失败”。。。
nichilemaworld 2014-11-13
  • 打赏
  • 举报
回复
引用 12 楼 yanggang832 的回复:
[quote=引用 10 楼 nichilemaworld 的回复:] [quote=引用 9 楼 magi1201 的回复:] [quote=引用 7 楼 nichilemaworld 的回复:] 按您说的改了一下,为什么启动后直接显示“登陆失败”??
那说明你的username和password还至少有一个没有传到后台,检查下你的form表单字段的定义吧 使用上面的写法可以帮忙规避掉空指针问题,但并没有解决以前的没有接收到传值的问题 或者 把你的提交页面发出来吧,让大家帮你看看问题[/quote] <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.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> <form action="LoginServlet" method="post"> <input type="text" name="username"> <input type="password" name="password"> <input type="submit"> </form> </body> </html> 这个应该就是提交页面吧。。。。[/quote] 没有传值的操作吧[/quote] 我这是看的教学视频,教的第一个web程序,好多知识都还不懂?您能教我怎么修改下么??
姜小白- 2014-11-13
  • 打赏
  • 举报
回复
if ((null != username) && (null != password) && "lc".equals(username.trim()) && "123".equals(password.trim())) 进行判断 防止传进来的username 或者 password 前后有空字符的影响
yanggang832 2014-11-13
  • 打赏
  • 举报
回复
在后台输出username和password看看
yanggang832 2014-11-13
  • 打赏
  • 举报
回复
引用 10 楼 nichilemaworld 的回复:
[quote=引用 9 楼 magi1201 的回复:] [quote=引用 7 楼 nichilemaworld 的回复:] 按您说的改了一下,为什么启动后直接显示“登陆失败”??
那说明你的username和password还至少有一个没有传到后台,检查下你的form表单字段的定义吧 使用上面的写法可以帮忙规避掉空指针问题,但并没有解决以前的没有接收到传值的问题 或者 把你的提交页面发出来吧,让大家帮你看看问题[/quote] <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.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> <form action="LoginServlet" method="post"> <input type="text" name="username"> <input type="password" name="password"> <input type="submit"> </form> </body> </html> 这个应该就是提交页面吧。。。。[/quote] 没有传值的操作吧
yanggang832 2014-11-13
  • 打赏
  • 举报
回复
引用 8 楼 nichilemaworld 的回复:
[quote=引用 5 楼 yanggang832 的回复:] 根本就没把username和password的值传到后台吧
这个有个登陆界面的,应该是我输入值之后传到后台,但是不知道为什么直接无法运行[/quote] 我是觉得两个值都没有传到后台去
nichilemaworld 2014-11-13
  • 打赏
  • 举报
回复
引用 9 楼 magi1201 的回复:
[quote=引用 7 楼 nichilemaworld 的回复:] 按您说的改了一下,为什么启动后直接显示“登陆失败”??
那说明你的username和password还至少有一个没有传到后台,检查下你的form表单字段的定义吧 使用上面的写法可以帮忙规避掉空指针问题,但并没有解决以前的没有接收到传值的问题 或者 把你的提交页面发出来吧,让大家帮你看看问题[/quote] <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.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> <form action="LoginServlet" method="post"> <input type="text" name="username"> <input type="password" name="password"> <input type="submit"> </form> </body> </html> 这个应该就是提交页面吧。。。。
姜小白- 2014-11-13
  • 打赏
  • 举报
回复
引用 7 楼 nichilemaworld 的回复:
按您说的改了一下,为什么启动后直接显示“登陆失败”??
那说明你的username和password还至少有一个没有传到后台,检查下你的form表单字段的定义吧 使用上面的写法可以帮忙规避掉空指针问题,但并没有解决以前的没有接收到传值的问题 或者 把你的提交页面发出来吧,让大家帮你看看问题
nichilemaworld 2014-11-13
  • 打赏
  • 举报
回复
引用 5 楼 yanggang832 的回复:
根本就没把username和password的值传到后台吧
这个有个登陆界面的,应该是我输入值之后传到后台,但是不知道为什么直接无法运行
nichilemaworld 2014-11-13
  • 打赏
  • 举报
回复
引用 4 楼 magi1201 的回复:
if(username.equals("lc") && password.equals("123")) request.getParameter( ); 获取到的参数为String 类型的,如果参数不存在,那么会返回null,这样就出现空指针了 使用username 和 password 之前可以先判空,然后再调用equals 方法 或者 将常量写在前面,if("lc".equals(username) 来规避空指针问题 检查下参数的大小写问题,避免因大小写导致的空指针
按您说的改了一下,为什么启动后直接显示“登陆失败”??
nichilemaworld 2014-11-13
  • 打赏
  • 举报
回复
引用 3 楼 u011939453 的回复:
java.lang.NullPointerException // 空指针异常 bbk.LoginServlet.doGet(LoginServlet.java:22) // doGet方法的22行,不知道是不是这行if(username.equals("lc") && password.equals("123")) 换成if("lc".equals(username) && "123".equals(password))试试
按您说的改了一下,为什么启动后直接显示“登陆失败”??
yanggang832 2014-11-13
  • 打赏
  • 举报
回复
根本就没把username和password的值传到后台吧
姜小白- 2014-11-13
  • 打赏
  • 举报
回复
if(username.equals("lc") && password.equals("123")) request.getParameter( ); 获取到的参数为String 类型的,如果参数不存在,那么会返回null,这样就出现空指针了 使用username 和 password 之前可以先判空,然后再调用equals 方法 或者 将常量写在前面,if("lc".equals(username) 来规避空指针问题 检查下参数的大小写问题,避免因大小写导致的空指针
加载更多回复(3)

81,094

社区成员

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

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