简体   繁体   English

java servlet getparameter返回null

[英]java servlet getparameter return null

I have a page that uses a servlet 我有一个使用servlet的页面

<!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>

This is the page 这是页面

/*
 * 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);
    }
}

And this is the servlet, but in doPost Method request.getParameters always return null and i don't know why. 这是servlet,但是在doPost方法中request.getParameters总是返回null,我不知道为什么。 I tried to change to request.getAtributes but the result is the same. 我尝试将其更改为request.getAtributes,但结果相同。

Please help me. 请帮我。

Hi can you try the below 嗨,你可以尝试以下

    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"));

Form parameters are identified by name, not by ID. 表单参数是通过名称而不是ID标识的。 You should use the same value for both name and ID and in the getParameter() method. 您应在name和ID以及getParameter()方法中使用相同的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM