我的代码来自java eclipse keplero,我的项目是一个servlet(服务器),它通过XMLRequest与chrome(客户端)的扩展进行通信。我已经使用了GET和POST,并且servlet可以在case之间通信,但是request.getParameter在每种情况下都是null。
的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>XMLFinal</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
的index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method = "get" action = "main">
Data: <input type="text" name="data">
<input type="submit" value = "send">
</form>
</body>
</html>
main.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
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 netscape.javascript.JSObject;
/**
* Servlet implementation class main
*/
@WebServlet("/main")
public class main extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public main() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
//server funziona in invio, il segnale in ricezione non funziona ancora
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//guardare file index.html
System.out.println(request.getParameter("data"));
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
//
response.getWriter().write("GET");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//guardare file index.html
System.out.println(request.getParameter("data"));
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
//invio segnale funzionante
response.getWriter().write("POST2");
}
private void getHeadersInfo() {
// TODO Auto-generated method stub
}
}
我在登录页面上有所改进,以创建一个简单的表单并且运行完美但我需要来自js客户端的数据:
loadXMLRequest();
function loadXMLRequest()
{
var xmlhttp;
//da testare su Chrome Firefox Opera IE 7 o superiore
if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
//arrivo del segnale
xmlhttp.open("GET","http://localhost:8080/XMLFinal/main?ciao",true);
xmlhttp.setRequestHeader("Content-Type","text/plain");
alert("questi sono i dati da inviare : ciao"");
xmlhttp.send();
alert("Message sent");
//apertura pagina web risposta giunta ed in elaborazione
//window.open(xmlhttp.responseText);
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==0)
{
alert("request not initialized");
}
if (xmlhttp.readyState==1)
{
alert("server connection established");
}
if (xmlhttp.readyState==2)
{
alert("request received");
}
if (xmlhttp.readyState==3)
{
alert("processing request");
}
if (xmlhttp.readyState==4)
{
alert("request finished and response is ready");
alert(xmlhttp.responseText);
//nessun dato ricevuto possibile shutdown del server
if(xmlhttp.responseText=="")
{
}
else
{
//windows.open(xmlhttp.responseText);
}
}
if (xmlhttp.readyState==200)
{
alert("OK");
alert(xmlhttp.responseText);
}
if (xmlhttp.readyState==404)
{
alert("Page not found");
}
}
}
else
{
//per IE 6 o inferiore
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
alert("Microsoft alert");
}
}
答案 0 :(得分:1)
您的代码说
System.out.println(request.getParameter("http"));
并且您的表单上写着
<input type="text" name="data">
将第一个更改为
System.out.println(request.getParameter("data"));
或第二个
<input type="text" name="http">
答案 1 :(得分:0)
你正在接听电话,即
xmlhttp.open("GET","http://localhost:8080/XMLFinal/main?ciao",true);
因此,从servlet调用doGet(-,-)
方法,但是在doGet(-,-)
方法下你已经编写了
System.out.println(request.getParameter("http"));
应该替换为
System.out.println(request.getParameter("data"));
您已使用doPost(-,-)
方法正确编写。
但我总是不想写冗余代码。在doGet()
和{ doPost()
,您可以在常用方法中编写代码,并从do ***方法调用相同的方法。即,
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
希望这会对你有所帮助。