2 u013621511 u013621511 于 2014.04.10 14:52 提问

String action = request.getParameter("action");action老是为空

myeclipse环境搭建一个服务器,文件时ClassInfoServlet,debug时候String action = request.getParameter("action");这句action为空,不知道怎么解决。。。
package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

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

import org.json.JSONStringer;

import dao.ClassInfoDAO;

import domain.Grade;

public class ClassInfoServlet extends HttpServlet{

private static final long serialVersionUID = 1L;

private ClassInfoDAO classinfodao = new ClassInfoDAO();
/**
 * @see HttpServlet#HttpServlet()
 */
public ClassInfoServlet() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    this.doPost(request, response);
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    String action = request.getParameter("action");
    if(action.equals("query")) {

        String classNumber = request.getParameter("classNumber");
        classNumber = classNumber==null?"":new String(request.getParameter("classNumber").getBytes("iso-8859-1"),"UTF-8");

        String className = request.getParameter("className");
        className = className==null?"":new String(request.getParameter("className").getBytes("iso-8859-1"),"UTF-8");

        String classSpecialFieldNumber = request.getParameter("classSpecialFieldNumber");
        classSpecialFieldNumber = classSpecialFieldNumber==null?"":new String(request.getParameter("classSpecialFieldNumber").getBytes("iso-8859-1"),"UTF-8");

        String classTeacherCharge = request.getParameter("classTeacherCharge");
        classTeacherCharge = classTeacherCharge==null?"":new String(request.getParameter("classTeacherCharge").getBytes("iso-8859-1"),"UTF-8");

        String classTelephone = request.getParameter("classTelephone");
        classTelephone = classTelephone==null?"":new String(request.getParameter("classTelephone").getBytes("iso-8859-1"),"UTF-8");

        List<Grade> GradeList = classinfodao.QueryClassInfo(classNumber,className, classSpecialFieldNumber, classTeacherCharge,classTelephone);
        StringBuffer sb = new StringBuffer();
        sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>").append("\r\n").append("<ScoreInfo>").append("\r\n");
        for(int i = 0; i<GradeList.size(); i++) {
            sb.append(" <ClassInfo>").append("\r\n")
                .append("       <classNumber>").append(GradeList.get(i).getclassNumber()).append("</classNumber>").append("\r\n")
                .append("       <className>").append(GradeList.get(i).getclassName()).append("</className>").append("\r\n")
                .append("       <classSpecialFieldNumber>").append(GradeList.get(i).getclassSpecialFieldNumber()).append("</classSpecialFieldNumber>").append("\r\n")
                .append("       <classTeacherCharge>").append(GradeList.get(i).getclassTeacherCharge()).append("</classTeacherCharge>").append("\r\n")
                .append("       <classTelephone>").append(GradeList.get(i).getclassTelephone()).append("</classTelephone>").append("\r\n")
            .append("   </ClassInfo>").append("\r\n");
        }
        sb.append("</ClassInfo>").append("\r\n");

        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        out.print(sb.toString());
    } else if(action.equals("add")) {
        String studentNumber=new String(request.getParameter("classNumber").getBytes("iso-8859-1"),"UTF-8");  
        Grade Grade = new Grade();

        Grade.setstudentNumber(studentNumber);
        ClassInfoDAO classinfodao = new ClassInfoDAO();
        String result = classinfodao.AddClassInfo(Grade);
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        out.print(result);
    } else if(action.equals("delete")) {
        String classNumber = request.getParameter("classNumber");
        ClassInfoDAO classinfodao = new ClassInfoDAO();

        String result = classinfodao.DeleteClassInfo(classNumber);
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        out.print(result);

    } else if(action.equals("updateQuery")) {
        String classNumber = request.getParameter("classNumber");
        ClassInfoDAO classinfodao = new ClassInfoDAO();
        Grade Grade = classinfodao.GetClassInfo(classNumber);
        //客户端查询对象信息,返回json数据格式
        //将List<Person>组织成JSON字符串   
        JSONStringer stringer = new JSONStringer();  
        try{  
            stringer.array();   
            stringer.object();
            stringer.key("classNumber").value(Grade.getclassNumber());
            stringer.key("className").value(Grade.getclassName());
            stringer.key("classSpecialFieldNumber").value(Grade.getclassSpecialFieldNumber());
            stringer.key("classTeacherCharge").value(Grade.getclassTeacherCharge());
            stringer.key("classTelephone").value(Grade.getclassTelephone());

            stringer.endObject();
            stringer.endArray();  
        }  
        catch(Exception e){}  
        response.getOutputStream().write(stringer.toString().getBytes("UTF-8"));  
        response.setContentType("text/json; charset=UTF-8");  //JSON的类型为text/json

    } else if(action.equals("update")) {
        //真正执行更新业务
        String classNumber = request.getParameter("classNumber");
        String className = request.getParameter("className");
        String classSpecialFieldNumber = request.getParameter("classSpecialFieldNumber");
        String classTeacherCharge = request.getParameter("classTeacherCharge");
        String classTelephone = request.getParameter("classTelephone");

        Grade Grade = new Grade();

        Grade.setclassNumber(classNumber);
        Grade.setclassName(className);
        Grade.setclassSpecialFieldNumber(classSpecialFieldNumber);
        Grade.setclassTeacherCharge(classTeacherCharge);
        Grade.setclassTelephone(classTelephone);


        ClassInfoDAO classinfodao = new ClassInfoDAO();
        String result = classinfodao.UpdateClassInfo(Grade);
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        out.print(result);
    } else if(action.equals("GetClassInfo")) {
        String classNumber = request.getParameter("classNumber");
        ClassInfoDAO classinfodao = new ClassInfoDAO();
        Grade Grade = classinfodao.GetClassInfo(classNumber);  
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Type", "text/plain;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.print(Grade.getclassNumber());
        out.print(Grade.getclassName());
        out.print(Grade.getclassSpecialFieldNumber());
        out.print(Grade.getclassTeacherCharge());
        out.print(Grade.getclassTelephone());


    }

}

}

2个回答

xiangnan129
xiangnan129   Rxr 2014.04.10 20:14

首先你确定你的那个name=“action”的属性值随着你的请求带过来了,再其次就是你的名字确定是“action”,最后就是有可能你的action这个名字是个敏感词,如果前两点都没错,那你换一个名字看看,注意换的时候,需要前后台名字一致。

biying00
biying00   2014.04.11 10:08

前台jsp name属性为 action 确定有吗?有的话 servlet收到的不可能是null。顶多为“”,或者action在jsp中被放到 form外了。。。。最好提供下jsp代码啊

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!