JavaWeb求教大神,为什么我这个会报错?? javawebjavaj2ee 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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);}在这个方法里加个断点看看username和password的值有没有传过来 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))试试 if(username.equals("lc") && password.equals("123"))request.getParameter( ); 获取到的参数为String 类型的,如果参数不存在,那么会返回null,这样就出现空指针了使用username 和 password 之前可以先判空,然后再调用equals 方法或者 将常量写在前面,if("lc".equals(username) 来规避空指针问题检查下参数的大小写问题,避免因大小写导致的空指针 根本就没把username和password的值传到后台吧 那说明你的username和password还至少有一个没有传到后台,检查下你的form表单字段的定义吧使用上面的写法可以帮忙规避掉空指针问题,但并没有解决以前的没有接收到传值的问题或者 把你的提交页面发出来吧,让大家帮你看看问题 那说明你的username和password还至少有一个没有传到后台,检查下你的form表单字段的定义吧使用上面的写法可以帮忙规避掉空指针问题,但并没有解决以前的没有接收到传值的问题或者 把你的提交页面发出来吧,让大家帮你看看问题<%@ 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>这个应该就是提交页面吧。 那说明你的username和password还至少有一个没有传到后台,检查下你的form表单字段的定义吧使用上面的写法可以帮忙规避掉空指针问题,但并没有解决以前的没有接收到传值的问题或者 把你的提交页面发出来吧,让大家帮你看看问题<%@ 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>这个应该就是提交页面吧。没有传值的操作吧 在后台输出username和password看看 if ((null != username) && (null != password) && "lc".equals(username.trim()) && "123".equals(password.trim())) 进行判断防止传进来的username 或者 password 前后有空字符的影响 那说明你的username和password还至少有一个没有传到后台,检查下你的form表单字段的定义吧使用上面的写法可以帮忙规避掉空指针问题,但并没有解决以前的没有接收到传值的问题或者 把你的提交页面发出来吧,让大家帮你看看问题<%@ 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>这个应该就是提交页面吧。没有传值的操作吧我这是看的教学视频,教的第一个web程序,好多知识都还不懂?您能教我怎么修改下么?? String username = request.getParameter("username");String password = request.getParameter("password");System.out.println("username=" + username);System.out.println("password=" + password );看看需要的username和password有没有传递到后台 就加到你后台的servlet里面结果会在tomcat控制台打印出来,如果是eclipse集成的tomcat,那就在eclipse的控制台打印出来 看到了,tomcat控制台显示这两个值都是null,这是怎么造成的?问题是不是出在登陆界面上??应该怎么修改下?? 我自己把问题解决了!哈哈!我相信楼上各位大神做梦都没想到我这个新手白痴是怎么犯的错我应该先在浏览器运行login.jsp的,而不是上来直接运行servlet,作为个新手真是太白痴了,还是感谢楼上各位今晚的付出,谢谢你们了! web问题!! struts在jsp页面使用ajax报错,请帮忙看一下啊,谢谢各位! 火狐浏览器从高于3.5版本以后对struts2令牌不支持? 跪求!!!快交毕业设计了,还差一篇英文的学术论文。 如何通过按钮保存当前页面 在struts框中的分页传参问题 局域网办公系统短信问题? jsp高手请进 怎么让网页上的内容自动折行 jdbc问题,救命,急!!!!! hibernate入门程序出错,不能执行语句? 请教一个关于struts2的ModelDriven的变量名问题
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);
}
在这个方法里加个断点看看
username和password的值有没有传过来
bbk.LoginServlet.doGet(LoginServlet.java:22) // doGet方法的22行,不知道是不是这行if(username.equals("lc") && password.equals("123"))
换成if("lc".equals(username) && "123".equals(password))试试
request.getParameter( ); 获取到的参数为String 类型的,如果参数不存在,那么会返回null,这样就出现空指针了
使用username 和 password 之前可以先判空,然后再调用equals 方法
或者 将常量写在前面,if("lc".equals(username) 来规避空指针问题
检查下参数的大小写问题,避免因大小写导致的空指针
使用上面的写法可以帮忙规避掉空指针问题,但并没有解决以前的没有接收到传值的问题
或者 把你的提交页面发出来吧,让大家帮你看看问题
使用上面的写法可以帮忙规避掉空指针问题,但并没有解决以前的没有接收到传值的问题
或者 把你的提交页面发出来吧,让大家帮你看看问题
<%@ 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>
这个应该就是提交页面吧。
使用上面的写法可以帮忙规避掉空指针问题,但并没有解决以前的没有接收到传值的问题
或者 把你的提交页面发出来吧,让大家帮你看看问题
<%@ 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>
这个应该就是提交页面吧。
没有传值的操作吧
防止传进来的username 或者 password 前后有空字符的影响
使用上面的写法可以帮忙规避掉空指针问题,但并没有解决以前的没有接收到传值的问题
或者 把你的提交页面发出来吧,让大家帮你看看问题
<%@ 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>
这个应该就是提交页面吧。
没有传值的操作吧我这是看的教学视频,教的第一个web程序,好多知识都还不懂?您能教我怎么修改下么??
String password = request.getParameter("password");
System.out.println("username=" + username);
System.out.println("password=" + password );
看看需要的username和password有没有传递到后台
结果会在tomcat控制台打印出来,如果是eclipse集成的tomcat,那就在eclipse的控制台打印出来
我相信楼上各位大神做梦都没想到我这个新手白痴是怎么犯的错我应该先在浏览器运行login.jsp的,而不是上来直接运行servlet,作为个新手真是太白痴了,还是感谢楼上各位今晚的付出,谢谢你们了!