[英]How to send JSON data to server and get JSON/XML as response using JSP
I am a beginner for web application. 我是Web应用程序的初学者。 I created a dynamic web project using java EE on glassfish server.
我在glassfish服务器上使用Java EE创建了一个动态Web项目。 Now I want to let client send data using json to server and receive data from server using json or xml.
现在,我想让客户端使用json将数据发送到服务器,并使用json或xml从服务器接收数据。 By searching online, I am now clear with how to program on server side.
通过在线搜索,我现在很清楚如何在服务器端进行编程。 Now I use ajax to send json data.
现在,我使用ajax发送json数据。 However, maybe it's to easy to code on the server, I can't find any code related to server.
但是,也许在服务器上编写代码很容易,我找不到与服务器相关的任何代码。 My server side should use JSP to read the json data, using a bean(finished) to generate some data and send the data back.
我的服务器端应使用JSP读取json数据,并使用bean(已完成)生成一些数据并将其发送回去。 Here is the code and I don't where is the problem.
这是代码,我不在哪里。 Can anyone give any advice to me?
谁能给我任何建议? your help means a lot for me!
您的帮助对我意义重大!
this is the ajax code on the client side. 这是客户端上的ajax代码。 I sent two input number from a form
我从表格发送了两个输入号码
$(function() {
$("#myform").submit(function() {
var lat = $("#num1").val();
var lon = $("#num2").val();
alert("form");
if (num1 == '' || num2 == '') {
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
} else {
$.ajax({
type : "POST",
url : "marker.jsp",
contenttype : 'application/json; charset=utf-8',
data : {
"num1" : "wtf",
"num2" : $("#num2").val(),
},
success : function(msg) {
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
alert(msg);
}
});
}
return false;
});
});
but after I switch to the jsp page, I only found two null values displayed, here's the code on the server, I planned to send xml at the beginning and I am not sure whether the request.getParameter can work and how to send back these xml data or using json format to send the data back. 但是切换到jsp页面后,我只发现显示了两个null值,这是服务器上的代码,我计划在开始时发送xml,但不确定request.getParameter是否可以工作以及如何发回这些值xml数据或使用json格式将数据发送回。 Help!
救命!
<?xml version="1.0" encoding="UTF-8"?>
<%@ page contentType="text/xml" %>
<%@ page import="javax.naming.InitialContext,net.roseindia.ejb3.stateless.*,javax.ejb.EJB,java.util.*"%>
<%
try {
String s1 = request.getParameter("num1");
String s2 = request.getParameter("num2");
%>
<%=s1%>
<%=s1%>
<%
if (s1 != null && s2 != null) {
List<String> textdatas = cal.GetTextResults(s1, s2);
for (String textdata : textdatas) {
String textLocation= "("+textdata.split("\\t",2)[0]+")";
System.out.println(textLocation);
%>
<text>
<location><%=textLocation%></location>
<event> <%=textdata.split("\\t",2)[1]%></event>
</text>
<%
}
List<String> images = cal.getImage();
for(String image: images){
System.out.println(image);
%>
<image>
<imglocation><%=image.split("\\t",2)[0]%></imglocation>>
<path><%=image.split("\\t",2)[0]%></path>
</image>
<%
}
}
}// end of try
catch (Exception e) {
e.printStackTrace();
//result = "Not valid";
}
%>
If you are not using any MVC framework I suggest you to adopt one. 如果您不使用任何MVC框架,建议您采用一个。 It doesn't seem that rendering an XML/JSON from a JSP is a good/easy practice.
从JSP渲染XML / JSON似乎不是一个好/容易的做法。 Its not difficult to see that your code its getting too cumbersome... not to say that it might not be valid code.
不难看出您的代码变得太麻烦了...并不是说它可能不是有效的代码。
I suggest that you adopt a MVC framework - I use VRaptor which is quite a good and simple one, allows you to develop RESTful web apps without even being aware of it. 我建议您采用一个MVC框架-我使用VRaptor ,它是一个非常简单的好方法,它使您无需知道就可以开发RESTful Web应用程序。 Make sure to check this one minute guide !!!
确保检查这一分钟指南 ! This framework by encapsulating ThoughWorks XStream Framework makes it dead simple to accomplish your task.
通过封装WhileWorks XStream Framework ,此框架使完成任务非常简单。 Take a look at what would be needed to respond to a request with a JSON object:
看一下用JSON对象响应请求所需要的内容:
import static br.com.caelum.vraptor.view.Results.*;
@Resource
public class ClientsController {
private final Result result;
private final ClientDao dao;
//result and dao parameters gets inject by VRaptor that
//by its turn lets you chose which Dependency Injection framework
//you wouldd like to be using with - pretty much as a plugin
public ClientsController(Result result, ClientDao dao) {
this.result = result;
this.dao = dao;
}
@Get("/client/json")
public void loadJson(MyParam param) {
result.use(json()).from(dao.getClient(param.id)).serialize();
}
@Get("/client/xml")
public void loadXml(MyParam param) {
result.use(xml()).from(dao.getClient(param.id)).serialize();
}
}
Note that Vraptor deserializes MyParam object from JSON and also injects it to your controller action request arrives! 请注意,Vraptor从JSON反序列化MyParam对象,并将其注入到您的控制器操作请求到达时!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.