Servlet没有给jsp输出

时间:2016-08-31 11:12:53

标签: jsp servlets

我的申请面临着一种奇怪的行为 我不明白为什么它显示错误,并且如果错误被修复它没有给出任何输出

任何人都可以帮我解决这个问题吗?

这是错误

HTTP Status 500 - 

--------------------------------------------------------------------------------

type Exception report

message 

description The server encountered an internal error that prevented it from fulfilling this request.

exception 

java.lang.NullPointerException
    com.arithmatic.pkg.Servlet01.doGet(Servlet01.java:33)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)


note The full stack trace of the root cause is available in the Apache Tomcat/7.0.34 logs.


--------------------------------------------------------------------------------

Apache Tomcat/7.0.34

这是我的servlet.java代码

package com.arithmatic.pkg;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Servlet01 extends HttpServlet {



@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {



        String Num1= request.getParameter("Num1");
        String Num2= request.getParameter("Num2");
        String op=request.getParameter("oper");

        if(Num1.equals("")||Num2.equals("")){
                PrintWriter out= response.getWriter();
                out.println(" Fields cant be left blank");
               request.getRequestDispatcher("Index.jsp").forward(request, response);
            }
            request.setAttribute("Num1", Num1);
            request.setAttribute("Num2", Num2);

          if(op.equals("Add")){

              int op1=Integer.parseInt((String)request.getAttribute("Num1"));
              int op2=Integer.parseInt((String)request.getAttribute("Num2"));
              int r=op1+op2;
              request.setAttribute("result",Integer.toString(r));
             request.getRequestDispatcher("Index.jsp").forward(request, response);


            }
          else if(op.equals("Substract")){
                 int op1=Integer.parseInt((String) request.getAttribute("Num1"));
                  int op2=Integer.parseInt((String)request.getAttribute("Num2"));
                  int r=op1-op2;
                  request.setAttribute("result",Integer.toString(r));
                  request.getRequestDispatcher("Index.jsp").forward(request, response);

                 }
          else if(op.equals("Multiply")){ 
              int op1=Integer.parseInt((String)request.getAttribute("Num1"));
              int op2=Integer.parseInt((String)request.getAttribute("Num2"));
              int r=op1*op2;
              request.setAttribute("result",Integer.toString(r));
             request.getRequestDispatcher("Index.jsp").forward(request, response);

         }
          else {
              int op1=Integer.parseInt((String)request.getAttribute("Num1"));
              int op2=Integer.parseInt((String)request.getAttribute("Num2"));
              int r=op1/op2;
              request.setAttribute("result",Integer.toString(r));
              request.getRequestDispatcher("Index.jsp").forward(request, response);
          }



    }

这是我的jsp代码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Giving the input</title>
</head>
<body>
<form  action ='Servlet01'>
         <center>Input No.<br>
        <input type="text" name="Num1" ><br>
        <br>
        <input type="text" name="Num2"  ><br>
        <br>
        Select the operation<br>
        <Select Name="oper" >   
            <option>Add</option>
            <option>Subtract</option>
            <option>Multiply</option>
            <option>Division</option>
        </Select>
        <br><br>
        <input type="submit" value="Calculate"><br> 
        </center>
        <br><br>
        Result=
        <br>
        <input type="text" name="done" value='<%=request.getAttribute("result")%>' /> 
</form>


    </body>
</html>

和web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Webapp-01</display-name>
  <welcome-file-list>
    <welcome-file>Index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Servlet01</servlet-name>
    <servlet-class>com.arithmatic.pkg.Servlet01</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Servlet01</servlet-name>
    <url-pattern>/Servlet01</url-pattern>
  </servlet-mapping>
</web-app>

1 个答案:

答案 0 :(得分:0)

堆栈跟踪的顶部显示:

java.lang.NullPointerException
    com.arithmatic.pkg.Servlet01.doGet(Servlet01.java:33)

如果我在Servlet01中正确计算了源代码行,则这是违规行:

int op2=Integer.parseInt((String)request.getAttribute("Num2"));

因此,似乎Num2的值是null,虽然我的计数可能已关闭(或者您的代码并不完全排列),因为我很惊讶异常没有不会发生在前一行,Num1

有两种可能的解决方法:

  1. 由于您使用的是GET而不是POST,因此请将对request.getAttribute的所有来电更改为request.getParameter,以便检索表单数据。

  2. 或者,请改用POST,这对于提交表单数据更为常规。这样做如下:

    1. method="POST"添加到您的初始表单标记中,如下所示:

      <form action="Servlet01" method="POST">

    2. 将您的servlet的doGet方法重命名为doPost(并保持对[{1}}的调用)。