如何从jsp通过post方式发送xml数据到servlet
展开全部
jsp页面 : index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>sendxml</title>
</head>
<body>
<form action="GetXml" method="post">
<textarea rows="20" cols="50" name="xml">
</textarea>
<input type="submit" value="提交XML">
</form>
</body>
</html>
2.servlet
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GetXml extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String xml=request.getParameter("xml");
System.out.println(xml);
}
}
3. web.xml 配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>GetXml</servlet-name>
<servlet-class>servlet.GetXml</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetXml</servlet-name>
<url-pattern>/GetXml</url-pattern>
</servlet-mapping>
</web-app>
已经给楼主调试并成功运行!
如果楼主按照上面的运行不出来。。
百度HI我。。我给你源程序。。。
祝楼主早日成功哈!
备注:接毕业设计,价钱面议!
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>sendxml</title>
</head>
<body>
<form action="GetXml" method="post">
<textarea rows="20" cols="50" name="xml">
</textarea>
<input type="submit" value="提交XML">
</form>
</body>
</html>
2.servlet
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GetXml extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String xml=request.getParameter("xml");
System.out.println(xml);
}
}
3. web.xml 配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>GetXml</servlet-name>
<servlet-class>servlet.GetXml</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetXml</servlet-name>
<url-pattern>/GetXml</url-pattern>
</servlet-mapping>
</web-app>
已经给楼主调试并成功运行!
如果楼主按照上面的运行不出来。。
百度HI我。。我给你源程序。。。
祝楼主早日成功哈!
备注:接毕业设计,价钱面议!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询