我在Netbeans和Tomcat上使用MCV模式技术。所以当我注册时我尝试填充我的数据库,但是我在doGet和doPost上得到Null异常。这里是代码:
控制器
public class Controller extends HttpServlet {
protected HashMap validActions = new HashMap();
@Override
public void init() throws ServletException {
try {
ResourceBundle bundle = ResourceBundle.getBundle("Action");
Enumeration e = bundle.getKeys();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
String value = bundle.getString(key);
try {
ActionHandler action = (ActionHandler) Class.forName(value).newInstance();
validActions.put(key, action);
} catch (Exception exc) {
exc.printStackTrace();
}
}
} catch (NullPointerException e) {
System.out.println(e);
} catch (MissingResourceException e) {
System.out.println(e);
} catch (Exception e) {
System.out.println(e);
}
}
//When the GET HTTP method is used, just call the doPost Method
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
//Check if the current event is a valid application Event
String action = validateAction(request);
//GET the appropriate event handler for the requested event. If you can't, then get Get the ERROR_HANDLER
ActionHandler handler = getActionHandler(action);
try {
//Ask the handler to process the request.
handler.process(getServletContext(), request, response);
} catch (Exception e) {
request.setAttribute("error", e);
handler = getActionHandler(Conf.ERROR);
}
//Forward the Response to the appropriate Viewer.
handler.forward(request, response);
}
//Method that validates and returns the event requrested by the HTTP Request
//If no event was set, then it returns UNKNOWN_EVENT
protected String validateAction(HttpServletRequest request) {
String action = request.getServletPath();
if (action == null || "" == action) {
return action = "HOME.show";
}else{
action = action.substring(1);
}
if (!validActions.containsKey(action)) {
action = Conf.UNKNOWN_ACTION;
}
return action;
}
/* Method that returns the assigned ActionHandler for a given Action
* If no ActionHandler is assigned, then it returns the ActionHandler of the
* Home.show action */
protected ActionHandler getActionHandler(String e) {
ActionHandler h;
try {
h = (ActionHandler) validActions.get(e);
} catch (Exception exc) {
h = (ActionHandler) validActions.get(Conf.UNKNOWN_ACTION);
}
return h;
}
}
SignUpActionHandlerDO
public class SignUpActionHandlerDO extends ActionHandler {
private String viewer= "/View/home.jsp";
private Person newPerson = null;
@Override
public String getViewer() {
return viewer;
}
@Override
public Object getModel() {
return newPerson;
}
public SignUpActionHandlerDO() {
}
@Override
public void process(ServletContext sc, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String Fname = request.getParameter("Fname");
String Lname = request.getParameter("Lname");
String email = request.getParameter("email");
String pass = request.getParameter("pass");
PersonManager PM = new PersonManager();
try{
newPerson = PM.createPerson(Fname,Lname,email,pass);
}catch(IOException ex){
viewer = "/View/signup.jsp";
request.setAttribute("error", ex.getMessage());
}
}
}
SignUp.jsp
<form class="form-signup" action="SIGNUPdo.show" method="POST">
at Controller.Control.Controller.doPost(Controller.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2463)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2452)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
所以关于这里有什么问题的想法?