gpt4 book ai didi

java servlet getparameter返回null

转载 作者:行者123 更新时间:2023-12-01 10:17:17 25 4
gpt4 key购买 nike

我有一个使用 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>

这是页面

/*
* 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 但结果是一样的。

请帮助我。

最佳答案

您好,您可以尝试以下操作吗

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

关于java servlet getparameter返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35821792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com