public class Messagebiz extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("ok");
HttpSession session = req.getSession();
System.out.println(session.toString());
resp.setContentType("text/html");
String type = req.getParameter("type");
Messagebean messagebean = new Messagebean();
Message message = new Message();
if (type.equals("insert")) {
message.setname(this.getHtml(req.getParameter("name")));
System.out.println(message.getname());
message.setmotify(this.getHtml(req.getParameter("motify")));
message.setcontext(this.getHtml(req.getParameter("context")));
System.out.println(message.getcontext()+" this is contest!");
if( message.getcontext()=="" ||message.getcontext()==null )
{session.setAttribute("message", "error");
resp.sendRedirect("error.jsp");
return ;
}
else
{
}
if ( messagebean.Insertmessage(message))
{
resp.sendRedirect("main.jsp");
}
else
{
resp.sendRedirect("error.jsp");
}
}
}
public void destroy() {
// TODO Auto-generated method stub
super.destroy();
}
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
}
public static String getHtml(String s) {
char c[] = s.toCharArray();
char ch;
StringBuffer buf = new StringBuffer();
for (int i = 0, size = c.length; i < size; i++) {
ch = c[i];
if (ch == '"') {
buf.append(""");
} else if (ch == '&') {
buf.append("&");
} else if (ch == '<') {
buf.append("<");
} else if (ch == '>') {
buf.append(">");
} else if (ch == '\n') {
// buf.append("<br>");
} else if (ch == ' ') {
buf.append(" ");
} else {
buf.append(ch);
}
}
c = null;
return buf.toString();
}
}
这个一服务器一运行它就报错,错误如下
java.lang.nullpointerexcetpion
com.jsp.servlet.Messagebiz.getHtml(Messagebiz java:76) 就是指message.setcontext(this.getHtml(req.getParameter("context")));
System.out.println(message.getcontext()+" this is contest!");
com.jsp.servlet.Messagebiz.doPost(Messagebiz.java:34)
运行结果里面可以输出message.name,但是不能输出message.getcontext() 求高手帮忙 ,小弟不胜感激!!
------解决方案--------------------------------------------------------
getHtml方法中
char c[] = s.toCharArray();
这句代码异常了,s可能为null,当你页面请求没有context参数的时候,像这样的问题设置个断点,debug调试,确定问题