首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

servlet中的if判断无法完成,该怎么解决

2012-02-10 
servlet中的if判断无法完成程序主要是学校的作业,但是在servlet中 if判断语句只能执行true的部分(用于页面

servlet中的if判断无法完成
程序主要是学校的作业,
但是在servlet中 if判断语句只能执行 true的部分(用于页面的跳转), false的就无法执行(用于执行sql的添加语句)
我认为是逻辑上的问题 主要是 这一句 u=ud.finduserBynumber(number);放置的问题

package servlet;

import java.io.IOException;
import java.io.PrintWriter;

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

import Dao.userDao;
import VO.user;

public class zhuce extends HttpServlet {

/**
* Constructor of the object.
*/
public zhuce() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
doPost(request, response);
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
response.setContentType("text/html");

String number=request.getParameter("number");
String password=request.getParameter("password");
String username=request.getParameter("username");
String name=request.getParameter("name");
String sex=request.getParameter("sex");
String age=request.getParameter("age");
String ldentitycard=request.getParameter("ldentitycard");

///////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
问题区域
/////////////////////////////////////
//////////////////////////////////////
user u=null;
userDao ud=new userDao();
//finduserBynumber()方法 通过number查询
u=ud.finduserBynumber(number);



PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");


out.println(" <BODY>");



if(number.equals(u.getNumber())){
//判断帐号是否重复,重复就跳转
RequestDispatcher rdp=request.getRequestDispatcher("repeat.html");
rdp.forward(request, response);
}else{
u.setNumber(number);
u.setPassword(password);
u.setUsername(username);
u.setName(name);
u.setSex(sex);
u.setAge(age);
u.setLdentitycard(ldentitycard);
ud.save(u);
 

out.println("帐号注册成功<br>");
out.println("<a href=denglu.html>如果你的浏览器不支持自动跳转请点解这里</a>");



}

///////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
////////////////////////////////////////////////////////

out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}

}


[解决办法]
if(number.equals(u.getNumber())){ 这里不要这么判断 这些就执行不到 else 块部分 你括号中的返回值只有true 还有就是没有值抛NullPonitException程序结束 就执行不到else

你用number 去数据库中查有没有对应的u 如果有则u.getNumber() 返回的值跟number的值是一样的 如果没有u.getNumber() 就会报NullPonitException (u 是空的 再调用get方法就报错)

你换成if(u != null){....

应该是可以的

试试

热点排行