使用<a href=""></a>将值从jsp传递到servlet

时间:2012-08-07 14:00:56

标签: java jsp tomcat servlets href

我有jsp页面 -

<html>
<head>
</head>
<body>
      <a href="http://localhost:8080/MyProject/Servlet123?usrID=33333">Go to servlet</a>
</body>
</html>

和servlet -

@WebServlet("/Servlet123")
public class Servlet123 extends HttpServlet {
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // take the ID and retrive all his accounts..
        String usrID = request.getAttribute("usrID").toString();
          }

}

当我按下jsp页面中的链接时,它会抛出异常

java.lang.NullPointerException
行中的

- String usrID = request.getAttribute("usrID").toString();

其他细节 - 我使用Apache Tomcat 7.0 Tomcat7。

1 个答案:

答案 0 :(得分:3)

使用getParameter()代替getAttribute()