Java doPost()在使用getParameter()时有效,但是doGet()总是抛出null ...为什么?

时间:2015-09-10 19:45:23

标签: javascript java servlets

我有一个带有 doGet() doPost()功能的servlet。当我在 doPost()上使用 getParameter()时,它工作得很完美,但当我在 doGet()<中使用 getParameter()时/ strong>它无法获取参数......它说它们是空的。这些是我的代码。

HTML(index.html):

<!DOCTYPE html>
<html>
<head>
    <meta charset="ISO-8859-1">
    <title>JAVA-AJAX Test</title>
    <script type="text/javascript" src="ajaxfunctions.js"></script>
</head>
<body>
    <h1>Enviar POST mediante AJAX</h1>
    <input type="text" id="username" name="username" value="Puma" />
    <input type="password" id="password" name="password" value="Bernal" />
    <input type="button" id="sendpost" name="sendpost" value="Send POST" />
    <hr/>
    <h1>Enviar GET mediante AJAX</h1>
    <input type="text" id="firstname" name="firstname" value="Jose" />
    <input type="text" id="lastname" name="lastname" value="Bernal" />
    <input type="button" id="sendget" name="sendget" value="Send GET" />
</body>

JavaScript(ajaxfunctions.js):

window.onload = function()
{
    xhr = new XMLHttpRequest();

    //*************************************POST FUNCTIONS************************************************

    var sendpost = document.getElementById("sendpost");

    sendpost.onclick = function()
    {
        var username = document.getElementById("username");
        var password = document.getElementById("password");

        if (username.value == "" || password.value == "")
            alert("Debe ingresar el usuario y la contraseña!");
        else
            enviarDatosPost(username, password);
    }

    function enviarDatosPost(username, password)
    {
        xhr.onreadystatechange = prepararRespuestaPost;
        xhr.open("POST", "Messages", true);
        var datosPost = crearDatosPost(username, password);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        alert("POST: " + datosPost);
        xhr.send(datosPost);
    }

    function crearDatosPost(username, password)
    {
        var datosPost = "username="+username.value+"&password="+password.value;
        return datosPost;
    }

    function prepararRespuestaPost()
    {
        if (xhr.readyState == 4)
        {
            if (xhr.status == 200)
            {
                alert("XHR responseText: " + xhr.responseText + "\nXHR statusText: " + xhr.statusText);
            }
        }
    }

    //*************************************GET FUNCTIONS************************************************

    var sendget = document.getElementById("sendget");

    sendget.onclick = function()
    {
        var firstname = document.getElementById("firstname");
        var lastname = document.getElementById("lastname");

        if (firstname.value == "" || lastname.value == "")
            alert("Debe ingresar el nombre y el apellido!");
        else
            enviarDatosGet(firstname, lastname);
    }

    function enviarDatosGet(firstname, lastname)
    {
        xhr.onreadystatechange = prepararRespuestaGet;
        xhr.open("GET", "Messages", true);
        var datosGet = crearDatosGet(firstname, lastname);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        alert("GET: " + datosGet);
        xhr.send(datosGet);
    }

    function crearDatosGet(firstname, lastname)
    {
        var datosGet = "firstname="+firstname.value+"&lastname="+lastname.value;
        return datosGet;
    }

    function prepararRespuestaGet()
    {
        if (xhr.readyState == 4)
        {
            if (xhr.status == 200)
            {
                alert("XHR responseText: " + xhr.responseText + "\nXHR statusText: " + xhr.statusText);
            }
        }
    }
}

Java(Messages.java):

package com.puma.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.*;
/**
 * Servlet implementation class Messages
 */
@WebServlet(asyncSupported = true, urlPatterns = { "/Messages" })
public class Messages extends HttpServlet
{
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Messages()
    {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        String responseData = "";
        String firstname = request.getParameter("firstname");
        String lastname = request.getParameter("lastname"); 
        System.out.println("DATOS - First Name: " + firstname + " / Last name: " + lastname);
        if (firstname.equals("Jose") && lastname.equals("Bernal"))
            responseData = "USUARIO CONOCIDO - Name : " + firstname + " / Last name: " + lastname;
        else
            responseData = "USUARIO DESCONOCIDO - Name: " + firstname + " / Last name: " + lastname;

        System.out.println(responseData);

        OutputStream stream = response.getOutputStream();
        OutputStreamWriter writer = new OutputStreamWriter(stream, "UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/plain");
        writer.write(responseData);
        writer.flush();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        String responseData = "";
        String user = request.getParameter("username");
        String pass = request.getParameter("password");
        if (user.equals("Puma") && pass.equals("Bernal"))
            responseData = "DATOS CORRECTOS - Username : " + user + " / Password: " + pass;
        else
            responseData = "DATOS INCORRECTOS - Username: " + user + " / Password: " + pass;

        System.out.println(responseData);

        OutputStream stream = response.getOutputStream();
        OutputStreamWriter writer = new OutputStreamWriter(stream, "UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/plain");
        writer.write(responseData);
        writer.flush();
    }
}

如果你看,POST和GET的结构完全相同。 POST工作完美,但GET没有。难道我做错了什么?提前谢谢!

0 个答案:

没有答案