的Java Servlet的getParameter返回null

问题描述:

我有一个使用一个servlet的Java Servlet的getParameter返回null

<!DOCTYPE html> 
<!-- 
To change this license header, choose License Headers in Project Properties. 
To change this template file, choose Tools | Templates 
and open the template in the editor. 
--> 

<html> 
    <head> 
     <title>Estoque Online - Registro</title> 
     <meta charset="UTF-8"> 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.1/css/materialize.min.css"> 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.1/js/materialize.min.js"></script> 
    </head> 
    <body> 
     <div> 
      <FORM ACTION="ValidaRegistro" method="post"> 
      <label> 
       <font size="6"> 
       Cadastro de Usuários 
       </font> 
      </label><br> 
      <label for="Nome"> 
       <font size ="4"> 
       Nome 
       </font> 
      </label> 
      <input autocomplete="on" type="Text" name="inNome" id="Nome"> 
      <label for="Email"> 
       <font size="4"> 
       Email 
       </font> 
      </label> 
      <input autocomplete="on" type="Text" name="inEmail" id="Email"> 
      <label for="Senha"> 
       <font size="4"> 
       Senha 
       </font> 
      </label> 
      <input type="password" name="inSenha" id="Senha">  
      <label for="Tipo"> 
       <font size="4"> 
       Tipo 
       </font> 
      </label> 
      <input type="Text" name="inTipo" id="Tipo"> 
      <input type="submit" id="Submeter" value="Submeter" class="btn waves-effect waves-light col s12"> 
      </FORM> 
     </div> 
    </body> 
</html> 

这是网页

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

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; 
import javax.swing.JOptionPane; 
import java.sql.*; 

import java.io.IOException; 
import java.io.PrintWriter; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.servlet.annotation.WebServlet; 

/** 
* 
* @author Rafael 
*/ 
/** 
* 
* @author Rafael 
*/ 

public class ValidaRegistro extends HttpServlet { 


    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> 
    * methods. 
    * 
    * @param request servlet request 
    * @param response servlet response 
    * @throws ServletException if a servlet-specific error occurs 
    * @throws IOException if an I/O error occurs 
    */ 
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     response.setContentType("text/html;charset=UTF-8"); 
     try (PrintWriter out = response.getWriter()) { 
      /* TODO output your page here. You may use following sample code. */ 
      out.println("<!DOCTYPE html>"); 
      out.println("<html>"); 
      out.println("<head>"); 
      out.println("<title>Servlet ValidaRegistro</title>");    
      out.println("</head>"); 
      out.println("<body>"); 
      out.println("<h1>Servlet ValidaRegistro at " + request.getContextPath() + "</h1>"); 
      out.println("</body>"); 
      out.println("</html>"); 
     } 
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> 
    /** 
    * Handles the HTTP <code>GET</code> method. 
    * 
    * @param request servlet request 
    * @param response servlet response 
    * @throws ServletException if a servlet-specific error occurs 
    * @throws IOException if an I/O error occurs 
    */ 
    @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     processRequest(request, response); 
    } 

    /** 
    * Handles the HTTP <code>POST</code> method. 
    * 
    * @param request servlet request 
    * @param response servlet response 
    * @throws ServletException if a servlet-specific error occurs 
    * @throws IOException if an I/O error occurs 
    */ 
    @Override 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     String Nome = (String)request.getParameter("Nome"); 
     String Email =(String)request.getParameter("Email"); 
     String Senha =(String)request.getParameter("Senha"); 
     int Tipo = Integer.parseInt(request.getParameter("Tipo")); 
     DAO.main(null); 

     try { 
      Inserir(Nome,Email,Senha,Tipo); 
     } catch (SQLException ex) { 
      Logger.getLogger(ValidaRegistro.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    /** 
    * Returns a short description of the servlet. 
    * 
    * @return a String containing servlet description 
    */ 
    @Override 
    public String getServletInfo() { 
     return "Short description"; 
    }// </editor-fold> 

    public void Inserir(String Nome,String Email,String Senha,int Tipo) throws SQLException{ 
     DAO.Inserir(Nome,Email,Senha,Tipo); 
    } 
} 

这是servlet,但在doPost方法request.getParameters总是返回null页我不知道为什么。 我试图改变request.getAtributes,但结果是一样的。

请帮帮我。

+0

你使用的是什么网址? –

+0

http:// localhost:8080/EstoqueOnline/Registro.html这是调用servlet的URL –

+0

您确定您正在正确执行POST吗?有太多事情可能会出错。 –

你好你可以试试下面的

String Nome = (String)request.getParameter("inNome"); 
    String Email =(String)request.getParameter("inEmail"); 
    String Senha =(String)request.getParameter("inSenha"); 
    int Tipo = Integer.parseInt(request.getParameter("inTipo")); 
+0

我尝试,但仍然出现空 –

表单参数是由名称标识,没有标识。对于名称和ID以及getParameter()方法,您应该使用相同的值。

+0

我也试过,但仍然出现空 –