Servlet:CheckUser.java
public class CheckUser extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
HttpSession session=request.getSession(true);
String name = (String) request.getAttribute("username");
String password = (String) request.getAttribute("password");
System.out.println(name+"="+password);
}
}JSP页面:Welcom.jsp
<%@ page language="java" 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 'Welcom.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="<%=request.getContextPath()%>/CheckUser.do" method="post">
<h3>登录</h3>
用户名:<input type="text" name="username" />
密码:<input type="password" name="password" />
<input type="submit" value="登录" />
</form>
</body>
</html>Servlet里面打印出来的总是NULL,报空指针异常,还请各位大侠赐教,感激不敬~~~
public class CheckUser extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
HttpSession session=request.getSession(true);
String name = (String) request.getAttribute("username");
String password = (String) request.getAttribute("password");
System.out.println(name+"="+password);
}
}JSP页面:Welcom.jsp
<%@ page language="java" 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 'Welcom.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="<%=request.getContextPath()%>/CheckUser.do" method="post">
<h3>登录</h3>
用户名:<input type="text" name="username" />
密码:<input type="password" name="password" />
<input type="submit" value="登录" />
</form>
</body>
</html>Servlet里面打印出来的总是NULL,报空指针异常,还请各位大侠赐教,感激不敬~~~
怎么这样用,你用的是什么框架啊?
String password = (String) request.getAttribute("password");
改成request.getParameter("username");
既然你用了action和submit提交,那么你再用一个form也没有其他的关系。而且还要安全一些。
是用来接受来自get方法或post方法的参数
<form method=post>
<form method=get>
<a href="1.jsp?id=1">ok</a>
只能接受java.lang.String
也就是说String hotel_id = request.getParameter("hotel_id"); request.getAttribute
是用来接受来自servlet的变量或Action(其实Action就是特殊的Servlet)
在Action中,request.setAttribute("ret",ret);
只能接受java.lang.Object
也就是说List ret = (List)request.getAttribute("ret"); 一个是变量类型,一个对象类型
如果直接用的MVC模式,就直接用 request.getParameter("username");request.getParameter("password");
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
HttpSession session=request.getSession(true);
String name = request.getParameter("name");
String password = request.getParameter("password");
System.out.println(name+"="+password);
}
}
问题就是出在getParameter写成了getAttribute~~~
再次感谢~~
另外你对getSession()的方法的调用也是没用到的,去掉吧