package news;
import java.io.*;
import java.io.IOException;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class AdminAction extends HttpServlet{
PrintWriter out;
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException
{
response.setContentType("text/html;charset=gb2312");
out=response.getWriter();
HttpSession session=request.getSession(false);
int adminid=(Integer)session.getAttribute("adminid");
int power=(Integer)session.getAttribute("power");
StrEncode SE=new StrEncode();
String nid=request.getParameter("nid");
int nnid = Integer.parseInt(nid);
String cid=request.getParameter("cid");
String action=request.getParameter("action");
DBConn conn=new DBConn();
try{
NewsAdmin NA=new NewsAdmin();
if(action.equals("1")){
if (NA.canAdmin(adminid, nnid) || power > 10) {
NA.DeleNews(nnid);
response.sendRedirect("index.jsp");
}
else{
out.print("你无权操作!");
}
}
if(action.equals("2")){
int ccid=Integer.parseInt(cid);
if (power > 10) {
NA.DeleComm(ccid);
response.sendRedirect("shownews.jsp?nid="+nid);
}
else{
out.print("你无权操作!");
}
} }
catch(Exception e){
out.print("发表评论写入数据库失败!"+e.toString());
}
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException
{
doPost(request, response);
}}
这句话在cid不为空的情况下是可以转换的,否则就出现你的问题。过滤下为空的情况再转换。
int ccid=Integer.parseInt(cid);
这里面的nid或者cid可能是null值,你强制转换前先判断是否是null值
String cid=request.getParameter("cid");首先你的nid和cid是数值型的字符串么?例如 "111","222"之类的。。
会不会出现"aaa","bbb"之类不能转成int型的字符串,要是有的话,只能try catch处理了还有你的nid和cid是否可能会空字符串""呢,
那就需要再转之前判断一下,比如
if (nid != null && !"".equals(nid)) {
//转成int操作
}
int nnid = Integer.parseInt(nid);
}catch (NullPointerException e) {
System.out.print(e+"空数据");
}catch (ParseException e) {
System.out.print(e+"数据错误")
}
int nnid = -1;
if(null != nid)//如果需要还可以trim一下...
nnid = Integer.parseInt(nid);
int power=(Integer)session.getAttribute("power");
这样转换行吗?