我正在使用 jquery ajax 来发送和接收来自 servlet 的数据。例如:
$(document).ready(function(){
$('button').click(function(){
$.post('/test/hello',{no:'5'},function (response){
$('#div').text(response);
});
});
});
Run Code Online (Sandbox Code Playgroud)
这是我在 hello.java servlet 中的代码
String no = request.getParameter("no");
int n = Integer.parseInt(no);
for (int i = 0; i < n; i++) {
out.println("hello<br/>");
}
Run Code Online (Sandbox Code Playgroud)
但是,当我收到 servlet 的响应时,hello 后跟的 Break 标记会在屏幕上连续一行打印 5 次,即 Break 标记不会解释为 html,而是解释为文本。
如何让 Jquery 将中断标记解释为新行,以便获得这样的输出?
你好
你好
你好
你好
你好
这是我在客户端的代码:
$.ajax({
type:'POST',
charset:'utf-8',
url:'http://localhost:8180/GisProject/MainService',
data:JSON.stringify(params),
success:function(msg)
{
console.log(msg);
},
error:function(xhr,status)
{
console.log(status);
},
contentType:"application/json"
});
Run Code Online (Sandbox Code Playgroud)
我之前已经在 Node 中使用 解析过这些数据express.bodyParser,但现在我必须使用 来解析它servlet。我看到人们在这里分配变量而不使用JSON.stringify并使用 获取该变量request.getParameter(myData)。
JSON将数据输入的标准方法是什么servlet?
为什么人们似乎在发送时Javascript objects将 JSON 作为字符串嵌入在 like 中data:{mydata:JSON.stringify(actualData)}?
如果我不清楚,我想使用该doPost方法的request对象来获取我从客户端发送的数据。
我正在学习 Apache Tomcat,但我不明白下面一行中的术语“实现” 。我认为 Tomcat运行Java Servlet 和 JavaServer Pages (JSP)。
Tomcat 实现 Sun Microsystems 的 Java Servlet 和 JavaServer Pages (JSP) 规范
Tomcat 是否实现或运行Java Servlet 和 JavaServer Pages (JSP)?
我有一个 JavaEE 应用程序,需要在运行时配置一些特定的系统属性。
在开发阶段,我们设置属性以.pom.xml使其工作:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<systemProperties>
<xxx>true</xxx>
</systemProperties>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
但是我想知道如果我们将应用程序部署在生产环境中怎么样?
我虽然在 servlet 初始化期间设置了该属性,但似乎一旦 jvm 运行我就无法修改系统属性(来自这篇文章):
Hotspot Java 实现的 JVM 内存参数只能在 JVM 启动时通过命令行选项进行设置。在 JVM 启动之前或之后在系统属性中设置它们将不会产生任何效果。
我尝试在web.xml(此处)中设置属性:
<env-entry>
<env-entry-name>xx</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>xx</env-entry-value>
</env-entry>
Run Code Online (Sandbox Code Playgroud)
但好像并没有什么影响。
然后我就想怎么解决呢?
顺便说一句,在生产环境中,我们可能会在共享 tomcat 下运行多个应用程序,因此我们更愿意仅在应用程序上下文下设置属性。
我是 Java 服务器端编程的新手,我的问题基本上是使用 Servlet 开始(低级别,不使用 spring mvc 等),然后从那里开始构建我的方式,来自 node.js 背景,其中路由定义将以函数( 等)开始app.get(request, response) {},app.post(request, response) {}该函数将接收requesthttpresponse方法之一(GET、POST、PUT、DELETE)的参数。
如果有人可以帮助我了解如何针对/usersservlet 类内的路由(比方说)定义方法,该类映射到 http 方法,同时在其参数中提供请求和响应。
我的尝试
public class FirstServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException , IOException {
}
Run Code Online (Sandbox Code Playgroud) 我试图获取 servlet 中的一些变量,这些变量都是通过 ajax 调用传递的。但我收到构造函数 JSONobject(string) 未定义错误。我也导入了所需的库。请帮忙
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.json.*;
public class insertserv extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
System.out.println("I am inside insert");
String json = "";
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
if (br != null) {
json = br.readLine();
}
System.out.println(json);
JSONObject wholedata= new JSONObject(json);
response.setContentType("application/json");
PrintWriter out= response.getWriter(); …Run Code Online (Sandbox Code Playgroud)在我的 ajax servlet 中,我设置了一个请求属性的对象列表。
request.setAttribute("testModelList",testList);
Run Code Online (Sandbox Code Playgroud)
在我的脚本中,我尝试检索属性并将其设置为隐藏变量。
// my JS
$('#testDetails').val() = '${testModelList}';
Run Code Online (Sandbox Code Playgroud)
我的jsp
<INPUT type="hidden" id = "testDetails" >
Run Code Online (Sandbox Code Playgroud)
但我收到错误
无法分配给函数结果
谁能帮我如何通过Ajax获取请求属性列表到JSP中的隐藏变量中?
我有一个.jsp页面,其形式如下:
<form name="myForm" method="post" action="LoginServlet">
<div class="form-group">
<label for="exampleXPID">userid</label>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1"><span class="glyphicon glyphicon-user"></span></span>
<input type="text" class="form-control" name="exampleXPID" id="exampleXPID" placeholder="XPID" required />
</div>
<span id="usernameError" style="color:red;"></span>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1"><span class="glyphicon glyphicon-star"></span></span>
<input type="password" class="form-control" name="exampleInputPassword1" id="exampleInputPassword1" placeholder="Password" required />
</div>
<span id="passwordError" style="color:red;"></span>
</div>
<hr/>
<button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-lock"></span>Login</button>
<a href="#" style="float:bottom; max-width: 100%;" data-toggle="popover" data-trigger="hover" data-content="Click here to reset your existing Password" data-placement="bottom"><span class="glyphicon glyphicon-question-sign"></span>Reset your …Run Code Online (Sandbox Code Playgroud) 我对以下行为感到困惑。变量标志从控制器传递到jsp代码:
flag: <c:out value="${requestScope.flag}"/>
flag eq 'Y': ${requestScope.flag eq 'Y'}
flag == 'Y': ${requestScope.flag=='Y'}
flag==Y: ${requestScope.flag==Y}
Run Code Online (Sandbox Code Playgroud)
情况1:标志未传递到视图(输出):
flag:
flag eq 'Y': false
flag == 'Y': false
flag==Y: true
Run Code Online (Sandbox Code Playgroud)
情况2:将值“ Y”的标志传递给视图:
flag: Y
flag eq 'Y': true
flag == 'Y': true
flag==Y: false
Run Code Online (Sandbox Code Playgroud)