-1

我的项目概念取决于下拉值,控件将转移到相关的 jsp 页面。

我创建了 4 个下拉值

1.安卓

2.struts

3.弹簧

4.休眠

和4个jsp页面

1.andriod.jsp

2.struts.jsp

3.spring.jsp

4.hibernate.jsp

如果我选择了 andriod 并单击提交按钮意味着控件将被转移到 andriod.jsp 页面,类似地其他值也被转移,

我的问题是

如果我选择 andriod 并单击提交按钮,它总是显示空指针异常...

请任何人解决我的问题。

我建立

1.jsp页面(文件名.jsp)

2.Servlet页面(CatServ.java)

新文件.jsp

<body>
<form action="test" method="post">
<table>
<tr>
<td>
Select the category</td>
<td>
<select>
  <option name="andriod"       value="lang">Andriod</option>
  <option name="java"           value="lang">Java</option>
  <option name="struts"         value="lang">Struts</option>
  <option name="spring"        value="lang">Spring</option>
  <option name="hybernate"   value="lang">Hybernate</option>
</select>
</td>
</tr>
<tr>
<td><center>
<input type="submit" value="Submit"></center>
</td>
</tr>
</table>
</form>
</body>

CatServ.java

package container;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CatServ
 */
@WebServlet("/CatServ")
public class CatServ extends HttpServlet
 {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        // TODO Auto-generated method stub
        String s=request.getParameter("lang");
        System.out.println(s);

        if(s.equals("andriod"))
        {
            RequestDispatcher  rd=request.getRequestDispatcher("AndriodServ.jsp");
            rd.forward(request, response);
        }
        else if(s.equals("java"))
        {
            RequestDispatcher  rd=request.getRequestDispatcher("JavaServ.jsp");
            rd.forward(request, response);
        }
        else if(s.equals("struts"))
        {
            RequestDispatcher  rd=request.getRequestDispatcher("StrutsServ.jsp");
            rd.forward(request, response);
        }
        else if(s.equals("spring"))
        {
            RequestDispatcher  rd=request.getRequestDispatcher("SpringServ.jsp");
            rd.forward(request, response);
        }
        else if(s.equals("hibernate"))
        {
            RequestDispatcher  rd=request.getRequestDispatcher("Hybernate.jsp");
            rd.forward(request, response);
        }
    }
}
4

2 回答 2

4

您混淆了名称和价值。你需要<select name="lang">和你的选择应该像<option value="spring">.

于 2012-12-29T15:44:49.390 回答
1

要正确工作,您必须重新编写您的选择,如下所示

<select name="lang">
 <option  value="andriod">Andriod</option>
<option  value="java">Java</option>
<option  value="struts">Struts</option>
<option  value="spring">Spring</option>
<option  value="hybernate">Hybernate</option>
</select>

您可以查看以获取更多信息

s另请注意,在调用之前检查 null是一种很好的做法equals

if(s !=null) {
// do my checks
}
于 2012-12-29T15:52:22.410 回答