package com.kingv.taskmanage.servlet;import java.io.IOException;
import java.sql.SQLException;import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.kingv.taskmanage.logic.PersonalManager;
import com.kingv.taskmanage.object.Personal;
import com.kingv.taskmanage.util.PageBean;public class UserServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
PersonalManager p = new PersonalManager();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { this.doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
String action = request.getParameter("action");
if(action==null){
this.list(request, response);//实现查询列表的方法
return;
}
if(action.equalsIgnoreCase("insert")){
try {
this.insert(request, response);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(action.equalsIgnoreCase("get")){
this.get(request, response);
}
if(action.equalsIgnoreCase("update")){
try {
this.update(request, response);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(action.equalsIgnoreCase("del")){
try {
this.delete(request, response);
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(action.equalsIgnoreCase("login")){
this.login(request, response);//这里会报错
}
}
public void list(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
String str2 =request.getParameter("pageNo");
//System.out.println(">>>>>>>>>>>"+str2);
int currentPage = 0;
if(str2!=null){
currentPage =Integer.parseInt(str2);
}
PageBean p1 =new PageBean();
p1.setCurrentPage(currentPage);
PageBean pb= p.getAll(p1);
pb.setCurrentPage(currentPage);
request.setAttribute("pageBean",pb);
RequestDispatcher rs =request.getRequestDispatcher("/user/userList.jsp");//参数是目标页面,相对路径
rs.forward(request,response);//这里也会报错
}
public void insert(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException, SQLException{
String userName = request.getParameter("userName");
String password = request.getParameter("password");
String type = request.getParameter("type");
Personal p1 = new Personal();
p1.setUserName(userName);
p1.setPassword(password);
p1.setType(Integer.parseInt(type));
boolean s = p.insert(p1);
if(s){
request.setAttribute("str", "新增用户成功");
}else{
request.setAttribute("str", "新增用户失败");
}
this.list(request, response); }
public void get(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
String id = request.getParameter("id");
Personal u =p.get(Integer.parseInt(id));
request.setAttribute("user",u);
RequestDispatcher rs = request.getRequestDispatcher("userEdit.jsp");
rs.forward(request,response); }
public void update(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException, SQLException{
String userName = request.getParameter("userName");
String password = request.getParameter("password");
String type = request.getParameter("type");
String id = request.getParameter("id");
Personal p1 = new Personal();
p1.setId(Integer.parseInt(id));
p1.setUserName(userName);
p1.setPassword(password);
p1.setType(Integer.parseInt(type));
boolean s = p.update(p1);
if(s){
request.setAttribute("str", "修改用户成功");
}else{
request.setAttribute("str", "修改用户失败");
}
this.list(request, response); }
public void delete(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException, NumberFormatException, SQLException{
String[] id = request.getParameterValues("id");
int i = 0 ;
for(String str:id){
boolean s = p.delete(Integer.parseInt(str));
if(s){
i++;
}
}
if(i==id.length){
request.setAttribute("str", "删除用户成功");
}else{
request.setAttribute("str", "删除用户失败");
}
this.list(request, response);
}
public void login(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
String userName = request.getParameter("userName");
String password = request.getParameter("password");
Personal per = p.login(userName, password);
if(per!=null){
request.getSession(true).setAttribute("person", per);
request.setAttribute("str", "登陆成功");
this.list(request, response);//这里也会报错
}else{
String str = "登录失败";
response.sendRedirect("/task/login.jsp?str="+str); }
}}
import java.sql.SQLException;import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.kingv.taskmanage.logic.PersonalManager;
import com.kingv.taskmanage.object.Personal;
import com.kingv.taskmanage.util.PageBean;public class UserServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
PersonalManager p = new PersonalManager();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { this.doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
String action = request.getParameter("action");
if(action==null){
this.list(request, response);//实现查询列表的方法
return;
}
if(action.equalsIgnoreCase("insert")){
try {
this.insert(request, response);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(action.equalsIgnoreCase("get")){
this.get(request, response);
}
if(action.equalsIgnoreCase("update")){
try {
this.update(request, response);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(action.equalsIgnoreCase("del")){
try {
this.delete(request, response);
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(action.equalsIgnoreCase("login")){
this.login(request, response);//这里会报错
}
}
public void list(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
String str2 =request.getParameter("pageNo");
//System.out.println(">>>>>>>>>>>"+str2);
int currentPage = 0;
if(str2!=null){
currentPage =Integer.parseInt(str2);
}
PageBean p1 =new PageBean();
p1.setCurrentPage(currentPage);
PageBean pb= p.getAll(p1);
pb.setCurrentPage(currentPage);
request.setAttribute("pageBean",pb);
RequestDispatcher rs =request.getRequestDispatcher("/user/userList.jsp");//参数是目标页面,相对路径
rs.forward(request,response);//这里也会报错
}
public void insert(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException, SQLException{
String userName = request.getParameter("userName");
String password = request.getParameter("password");
String type = request.getParameter("type");
Personal p1 = new Personal();
p1.setUserName(userName);
p1.setPassword(password);
p1.setType(Integer.parseInt(type));
boolean s = p.insert(p1);
if(s){
request.setAttribute("str", "新增用户成功");
}else{
request.setAttribute("str", "新增用户失败");
}
this.list(request, response); }
public void get(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
String id = request.getParameter("id");
Personal u =p.get(Integer.parseInt(id));
request.setAttribute("user",u);
RequestDispatcher rs = request.getRequestDispatcher("userEdit.jsp");
rs.forward(request,response); }
public void update(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException, SQLException{
String userName = request.getParameter("userName");
String password = request.getParameter("password");
String type = request.getParameter("type");
String id = request.getParameter("id");
Personal p1 = new Personal();
p1.setId(Integer.parseInt(id));
p1.setUserName(userName);
p1.setPassword(password);
p1.setType(Integer.parseInt(type));
boolean s = p.update(p1);
if(s){
request.setAttribute("str", "修改用户成功");
}else{
request.setAttribute("str", "修改用户失败");
}
this.list(request, response); }
public void delete(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException, NumberFormatException, SQLException{
String[] id = request.getParameterValues("id");
int i = 0 ;
for(String str:id){
boolean s = p.delete(Integer.parseInt(str));
if(s){
i++;
}
}
if(i==id.length){
request.setAttribute("str", "删除用户成功");
}else{
request.setAttribute("str", "删除用户失败");
}
this.list(request, response);
}
public void login(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
String userName = request.getParameter("userName");
String password = request.getParameter("password");
Personal per = p.login(userName, password);
if(per!=null){
request.getSession(true).setAttribute("person", per);
request.setAttribute("str", "登陆成功");
this.list(request, response);//这里也会报错
}else{
String str = "登录失败";
response.sendRedirect("/task/login.jsp?str="+str); }
}}
解决方案 »
- Java NullPointerException异常
- Java中是否有类似.Net中的timer控件?
- 在applicationContext.xml 增加data source 出错
- 求如何在action中获取spring配置文件中的数据库链接路径?
- java 多个图片上传。
- 请问 那个 面包屑导航条 怎么实现呢?
- 求java字符串截取代码
- jbuilder2005开发web services问题,郁闷了很久!请高手指教!
- J2EE tutorial servlet help!!
- 哪位大侠能讲讲怎么将一个用EJB写的站点做成安装程序?
- java 小题2个
- 用Java实现CA安全登录问题 SSH框架
严重: Servlet.service() for servlet UserServlet threw exception
java.lang.NullPointerException
at org.apache.jsp.user.userList_jsp._jspService(userList_jsp.java:183)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at com.kingv.taskmanage.servlet.UserServlet.list(UserServlet.java:48)
at com.kingv.taskmanage.servlet.UserServlet.doPost(UserServlet.java:29)
at com.kingv.taskmanage.servlet.UserServlet.doGet(UserServlet.java:23)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.kingv.taskmanage.servlet.EncodingFilter.doFilter(EncodingFilter.java:34)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:864)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1600)
at java.lang.Thread.run(Unknown Source)
严重: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at org.apache.jsp.user.userList_jsp._jspService(userList_jsp.java:65)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.kingv.taskmanage.servlet.EncodingFilter.doFilter(EncodingFilter.java:34)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:864)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1600)
at java.lang.Thread.run(Unknown Source)
下面是那个jsp页面
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="java.util.List"%>
<%@ page import="com.kingv.taskmanage.object.*" %>
<%@ page import="com.kingv.taskmanage.util.*"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<%
PageBean p =(PageBean) request.getAttribute("pageBean"); int pageNo = p.getCurrentPage();
List<Personal> list =p.getDataList();
%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
function sub(){
document.userForm.action="userServlet";
document.userForm.opr.value="del";
document.userForm.submit();
}
function checkAll(){
var h = document.getElementsByName("please");
var k = 0;
for(var i=0;i<h.length;i++){
if(h[i].checked){
k =1;
}
}
if(k==1){
var id = document.getElementsByName("id");
for(var j = 0 ; j< id.length;j++){
id[j].checked = true;
}
}else{
var id = document.getElementsByName("id");
for(var j = 0 ; j< id.length;j++){
id[j].checked = false;
}
}
}
</script>
</head><body onload="init()">
<form name="userForm" action="" method="get">
<input type="hidden" name="opr" value=""/>
<div id="dis">
<table bgcolor="#0033FF" align="center" width="800">
<tr bgcolor="#FFFFFF">
<td align="left">
<font style="font-family:'黑体';font-size:16px; color:red"><span id="str"></span></font>
</td>
</tr>
</table>
</div>
<table bgcolor="#0033FF" align="center" width="800">
<tr bgcolor="#FFFFFF">
<td colspan="5" align="center">
<font style="font-family:'黑体';font-size:16px; color:#0000CC">用户列表</font>
</td>
</tr>
<tr bgcolor="#FFFFFF" align="center">
<td align="left">
<font style="font-family:'黑体';font-size:12px; color:#0000CC"><input type="checkbox" name="please" value="1" onclick="checkAll()"/>请选择</font>
</td>
<td>
<font style="font-family:'黑体';font-size:12px; color:#0000CC">序号</font>
</td>
<td>
<font style="font-family:'黑体';font-size:12px; color:#0000CC">用户名</font>
</td>
<td>
<font style="font-family:'黑体';font-size:12px; color:#0000CC">用户性别</font>
</td>
<td>
<font style="font-family:'黑体';font-size:12px; color:#0000CC">是否修改</font>
</td>
</tr>
<%
for(int i=0;i<list.size();i++){
Personal u = list.get(i);
%>
<tr bgcolor="#FFFFFF">
<td>
<input type="checkbox" name="id" value="<%=u.getId()%>"/>
</td>
<td>
<font style="font-family:'黑体';font-size:12px; color:#000000"><%=i+1 %></font>
</td>
<td>
<font style="font-family:'黑体';font-size:12px; color:#000000"><%=u.getUserName() %></font>
</td>
<td>
<font style="font-family:'黑体';font-size:12px; color:#000000"><%=u.getType()==2?"领导":"一般职员" %></font>
</td>
<td align="center">
<a href="userServlet?action=get&id=<%=u.getId()%>"><font style="font-family:'黑体';font-size:12px; color:#000000">修改</font></a>
</td>
</tr>
<%
}
%>
</table>
<table bgcolor="#0033FF" align="center" width="800">
<tr bgcolor="#FFFFFF">
<td align="right">
<a href="#"><font style="font-family:'黑体';font-size:12px; color:#000000">上一页</font></a>
<a href="#"><font style="font-family:'黑体';font-size:12px; color:#000000">下一页</font></a>
<a href="userAdd.jsp"><font style="font-family:'黑体';font-size:12px; color:#000000">用户新增</font></a>
<a href="javascript:sub()"><font style="font-family:'黑体';font-size:12px; color:#000000">用户删除</font></a><%
if(pageNo>=1){
%>
<a href="userServlet?pageNo=<%=pageNo-1 %>"><font style="font-family:'黑体';font-size:12px; color:#000000">上一页</font></a>
<%}else{ %>
<a href="userServlet?pageNo=0"><font style="font-family:'黑体';font-size:12px; color:#000000">上一页</font></a>
<%} %>
<%
if(pageNo<p.getPageNum()-1){
%>
<a href="userServlet?pageNo=<%=pageNo+1 %>"><font style="font-family:'黑体';font-size:12px; color:#000000">下一页</font></a>
<%}else{ %>
<a href="userServlet?pageNo=<%=p.getPageNum()-1%>"><font style="font-family:'黑体';font-size:12px; color:#000000">下一页</font></a>
<% }%></td>
</tr>
</table>
</form>
</body>
</html>
下面是list
public void list(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
String str2 =request.getParameter("pageNo");
//System.out.println(">>>>>>>>>>>"+str2);
int currentPage = 0;
if(str2!=null){
currentPage =Integer.parseInt(str2);
}
PageBean p1 =new PageBean();
p1.setCurrentPage(currentPage);
String str = request.getParameter("outStr");
PageBean pb= p.getAll(p1);
pb.setCurrentPage(currentPage);
request.setAttribute("pageBean",pb);
RequestDispatcher rs = request.getRequestDispatcher("/user/userList.jsp");//参数是目标页面,相对路径
rs.forward(request,response);
}
下面是filter
public void doFilter(ServletRequest srequest, ServletResponse sresponse,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)srequest;
request.setCharacterEncoding(targetEncoding);
if("GET".equalsIgnoreCase(request.getMethod())) {
request = new MyRequest(request);
} else if("POST".equalsIgnoreCase(request.getMethod())) {
request.setCharacterEncoding(targetEncoding);
}
chain.doFilter(request,sresponse); //过滤后的请求转发*****这里会报空指针异常
}