LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
12
返回列表 发新帖
楼主: menglianjing

java空指针

[复制链接]
发表于 2004-5-8 17:58:40 | 显示全部楼层
/src/com/demo/user/UserBO.java请自己补充完整。。。
[PHP]

/*
* 创建日期 2003-11-2
* Copyright2003 hantsy<hantsy@163.net>
*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.


*/
package com.demo.user;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

import com.demo.ConnectionPool;

/**
* @author hantsy
*
* BO stand for Business Object,this file use for business transaction.
*
*/
public class UserBO {
private ConnectionPool pool;
   
public UserBO() {
  pool = ConnectionPool.getInstance();
}
   
  public User getUser(String userId){
      


  }


public Collection getAllUser(){
Connection con = null;
      
   try {        
     con = pool.getConnection(true);
           
     UserDAO userDAO = new UserDAO(con);
     return userDAO.findAllUser();
           
   } catch(Exception e) {
    System.err.println(e.getMessage());
    throw new RuntimeException("unexcepted");
   } finally {
    try {
      if(con != null) {
     con.close();
      }
    } catch(SQLException sqle) {
    System.out.println(sqle.getMessage());
    throw new RuntimeException("unexcepted");
    }
   }   

}
   
public void createUser(User user)
throws Exception {
      
  
  
  
      
}
   
public void updateUser(User user)
throws Exception {
      

      
}
   
public void removeUser(User user)
  throws Exception {
      
  
  }
   
   

}



[/PHP]
发表于 2004-5-8 17:59:46 | 显示全部楼层
/src/com/demo/user/UserServlet.java
[PHP]
/*
* 创建日期 2003-11-2
* Copyright2003 hantsy<hantsy@163.net>
*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.


*/
package com.demo.user;

import java.io.IOException;
import java.util.Collection;

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

/**
* @author hantsy
*
* This file use for process the client request...
*/
public class UserServlet extends HttpServlet {

public void destroy() {
//TODO Method stub generated by Lomboz
}

protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

protected void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String mode = request.getParameter("mode");
mode = (mode == null) ? "" : mode;
//System.out.println("process mode:"+mode);
UserBO userBO = new UserBO();

if (mode.equals("add")) {
  gotoPage(request, response, "/addUser.jsp");
} else if (mode.equals("create")) {
  String id = request.getParameter("userid");
  String password = request.getParameter("password");
  String email = request.getParameter("email");
  String role = request.getParameter("role");
  User user = new User();
  user.setId(id);
  user.setPassword(password);
  user.setEmail(email);
  user.setRole(role);

  try {
   userBO.createUser(user);
  } catch (Exception e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  }
  gotoPage(request, response, "/doUser?mode=list");

} else if (mode.equals("edit")) {
  String id = request.getParameter("id");
  User user = new User();
  user = userBO.getUser(id);
  request.setAttribute("user", user);
  gotoPage(request, response, "/editUser.jsp");
} else if (mode.equals("update")) {
  String id = request.getParameter("userid");
  String password = request.getParameter("password");
  String email = request.getParameter("email");
  String role = request.getParameter("role");
  User user = new User();
  user.setId(id);
  user.setPassword(password);
  user.setEmail(email);
  user.setRole(role);

  try {
   userBO.updateUser(user);
  } catch (Exception e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  }
  gotoPage(request, response, "/doUser?mode=list");

} else if (mode.equals("delete")) {
  String ids[] = request.getParameterValues("id");
  for (int i = 0; i < ids.length; i++) {
   try {
    userBO.removeUser(userBO.getUser(ids));
   } catch (Exception e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
   }
  }
  gotoPage(request, response, "/doUser?mode=list");

} else if (mode.equals("editmine")) {
  HttpSession session = request.getSession(true);
  User user = new User();
  user = (User) session.getAttribute("mine");
  request.setAttribute("user", user);
  gotoPage(request, response, "/editPersonal.jsp");
} else if (mode.equals("updatemine")) {
  String id = request.getParameter("userid");
  String password = request.getParameter("password");
  String email = request.getParameter("email");
  String role = request.getParameter("role");
  User user = new User();
  user.setId(id);
  user.setPassword(password);
  user.setEmail(email);
  user.setRole(role);

  try {
   userBO.updateUser(user);
  } catch (Exception e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  }
  gotoPage(request, response, "/");

}
else{//list all users...
  
  Collection list= userBO.getAllUser();
  request.setAttribute("userList",list);
  gotoPage(request,response,"/listUser.jsp");
  
}

}

private void gotoPage(
HttpServletRequest request,
HttpServletResponse response,
String url)
throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher(url);
dispatcher.forward(request, response);

}
}


[/PHP]
发表于 2004-5-8 18:01:00 | 显示全部楼层
/web/listUser.jsp完整的代码。。。。。
[PHP]

<!--
Copyright2003 hantsy<hantsy@163.net>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
-->
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<%@ page language="java" pageEncoding="UTF-8" import="com.demo.user.User,java.util.*"%>

<%
String path=request.getContextPath();
Collection list=(Collection)request.getAttribute("userList");

%>
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<title>User Administration</title>
</head>
<body>
<table width="100%" height="600" border="0">
<tr>
<td bgcolor="#CCCCCC" height="50">
<font face="Heveltica" style="font-size:25px">UserAdministration</font>
</td>
</tr>
<tr>
<td align="right">
<a href="<%=path%>/doUser?mode=add">Add</a>&nbsp;
<a href="<%=path%>/doUser?mode=list">Refresh</a>
</td>
</tr>
<tr>
<td>
<table cellspacing="1" cellpadding="5" border="0" bgcolor="#CCCCCC">
<tr bgcolor="#f8f8f8">
<td>USER ID</td>
<td>EMAIL</td><td>ROLE</td><td>ACTION</td></tr>
<%
Iterator iter=list.iterator();
while(iter.hasNext()){
User user=(User)iter.next();
%>
<tr bgcolor="#fffff">
<td><%=user.getId()%></td>
<td><%=user.getEmail()%></td>
<td><%=user.getRole()%></td>
<td><%
if(!user.getId().equals("test")&&!user.getId().equals("admin")){
%>
<a href="<%=path%>/doUser?mode=edit&id=<%=user.getId()%>">edit</a>&nbsp;
<a href="<%=path%>/doUser?mode=delete&id=<%=user.getId()%>">delete</a>
<%
}
%>
</td>
</tr>
<%}%>
</table>
</td>
</tr>
</table>

<br>
<br>
</body>
</html>

[/PHP]
发表于 2004-5-8 18:01:52 | 显示全部楼层
/web/WEB-INF/web.xml
[PHP]
<servlet>
       <servlet-name>UserServlet</servlet-name>
       <display-name>User servlet</display-name>
       <description>user admin</description>
       <servlet-class>com.demo.user.UserServlet</servlet-class>
</servlet>

<servlet-mapping>
       <servlet-name>UserServlet</servlet-name>
       <url-pattern>/doUser</url-pattern>
</servlet-mapping>


[/PHP]
发表于 2004-5-8 18:04:45 | 显示全部楼层
这段代码的设计已经包含了crud操作,java代码请自己补充完整,自己添加addUser.jsp,
editUser.jsp.这是jsp,serlet最基本的写法。。。没有用到struts,jstl...
这段代码是一次面试一个公司要求写的,摘录一部分。。。
 楼主| 发表于 2004-5-8 18:34:59 | 显示全部楼层

re

谢谢大家!因为只是测试,所以没有些池,谢谢大家
发表于 2004-5-9 13:26:22 | 显示全部楼层
楼主再不要贴代码了!

我看了你的代码 很多地方都有错误。你对  javabean  的理解本身就有问题。

关于javabean 你可以看看 《jsp从入门到精通》 这本书的实例代码。


错误:
1。程序 UserRegist 的第一行的 类的引用本身就是错误的。

2。UserInfo  和  UserRegist 本身是可以写再同一个javabean里面,你的两个类文件,反而把事情搞的更加复杂。

3。我实在是想不通,你怎么调试过 UserRegist 类的?你仔细看看,创建了对象,又有相同的对象再次被创建。

4。 UserInfo  和 UserRegist  类的方法重复,当然这个是没有什么关系的,那么你为什么要分开写呢?

最好想问问程序是你自己想的写的,还是部分借鉴别人的?

建议 重新思考,画出设计图示, 重新设计程序。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表