一个从html向servlet中提交数据的问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个从html向servlet中提交数据的问题相关的知识,希望对你有一定的参考价值。
请问怎么样用 submit() 方法将html中的数据提交到servlet中去呢
请举个简单的例子看看 谢谢!!!!!!!!!!!!
点击提交按钮,Tomcat后台输出:
control: aaa's value is : bbb
页面显示结果:
page: aaa's value is : bbb
文件目录结构:
test
│ test.html
│
└—WEB-INF
│ web.xml
└—classes
SubmitServle.java
SubmitServle.class
源代码(test.html):
<form action="submit.do">
<input type="textField" name="aaa" value="bbb" />
<input type="submit" />
</form>
源代码(web.xml):
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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 web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>submitServle</servlet-name>
<servlet-class>SubmitServle</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>submitServle</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
源代码(SubmitServle.java):
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 SubmitServle extends HttpServlet
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
String aaa = request.getParameter("aaa");
System.out.println("control: aaa's value is : " + aaa);
response.getOutputStream().println("page: aaa's value is : " + aaa);;
另:test.html中的提交按钮被点击时,会将表单中的字段aaa以及它的值提交到submit.do去(提交的这一过程也可以用javascript来写)。服务器接到这个请求,将解析web.xml文件中的内容,将转交给符合这一请求(*.do)的servlet--SubmitServlet处理。处理调用service方法,可以从request中取得参数aaa的值,然后打印出来。 参考技术A html:
<html>
<body>
<form action="LoginServlet" method="POST">
用户名:<input type="text" name="username">
密码: <input type="password" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>
action中的值与Servlet文件相同
Servlet:
package org.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 LoginServle extends HttpServlet
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("用户名:"+username);
System.out.println("密码:"+password);
修改web.xml文件
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>org.servlet.LoginServle</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServle</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>本回答被提问者采纳
如何从 servlet 向所有登录用户发送数据
【中文标题】如何从 servlet 向所有登录用户发送数据【英文标题】:How to send data from servlet to all logged In users 【发布时间】:2012-10-17 21:43:15 【问题描述】:我的要求是管理员应该向所有登录用户发送数据。我有一个 servlet 第一个用户发送登录 HTTP 请求并从 servlet 获取登录成功响应,现在该用户的会话已启动。类似地其他用户登录。现在管理员也是一个用户可以登录并看到两个用户 user1 和 user2 登录. 管理员应该向所有登录的用户发送数据。 Servlet 应该在没有用户 HTTP 请求的情况下将数据推送给该用户。请注意在会话期间用户不会发送任何其他 HTTP 请求。
我尝试存储每个登录用户的响应 obj 而不是彗星,后来使用该 obj 尝试将数据发回给用户,但没有成功。
请检查以下代码:LoginServlet.java
public class LoginServlet extends HttpServlet
private static final long serialVersionUID = 1L;
ServletContext sc;
protected ArrayList<HttpServletResponse> connections = new ArrayList<HttpServletResponse>();
public LoginServlet()
super();
public void init() throws ServletException
super.init();
sc = getServletContext();
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
synchronized (connections)
connections.add(response);
ServletContext sc = getServletContext();
Integer x = (Integer) sc.getAttribute("hit");
if (x == null)
x = 1;
else
x++;
sc.setAttribute("hit", x);
String username = request.getParameter("username");
String password = request.getParameter("password");
HttpSession session = request.getSession(true);
session.setAttribute("username", username);
session.setAttribute("password", password);
response.setContentType("text/html");
ArrayList<LoggedInUser> loggedInUsers = (ArrayList<LoggedInUser>) sc
.getAttribute("users");
if (loggedInUsers == null)
System.out.println("loggedInUsers creates");
loggedInUsers = new ArrayList<LoggedInUser>();
loggedInUsers.add(new LoggedInUser(username, password));
sc.setAttribute("users", loggedInUsers);
PrintWriter out = response.getWriter();
System.out.println("loggedInUsers ==>" + loggedInUsers.size());
if (username != null && username.equals("admin"))
out.println("Admin Login Success");
ServletContext scx = getServletContext();
Integer xx = (Integer) scx.getAttribute("hit");
out.println("<H1>Users visited " + xx + "</h1>");
loggedInUsers = (ArrayList<LoggedInUser>) sc.getAttribute("users");
for (int i = 0; i <= loggedInUsers.size() - 1; i++)
LoggedInUser LoggedInUser = loggedInUsers.get(i);
out.println(LoggedInUser.getmUsername()+ "<br>");
for (int i = 0; i < connections.size(); i++)
try
LoggedInUser LoggedInUser = loggedInUsers.get(i);
PrintWriter writer = connections.get(i).getWriter();
System.out.println("inside loope " + i + "\n" + writer);
writer.println("MESSAGE FROM ADMIN HERE" + "<br>");
writer.flush();
writer.close();
catch (IOException e)
log("IOExeption sending message", e);
else
out.println("Welcome " + username + " Login Success");
一旦用户登录arraylist中存储的resp obj。
synchronized (connections)
connections.add(response);
后来使用那个 obj 得到了 printwriter obj。
PrintWriter writer = connections.get(i).getWriter();
问题:
-
如何向所有登录的用户发送一条特定的消息?
没有彗星我们能做到吗?
如果我做错了什么,请更正代码并提供其他建议。谢谢
【问题讨论】:
嘿,你使用什么java-presentation-technology? Java-Server-Faces、Wicket、JSP、Struts、Spring? @peter 我们不能存储响应 obj 并使用该 obj 发送消息吗? @peter simple servlet-jsp using tomcat 当 servlet 的工作完成时,连接处于脱机状态!您应该将消息存储在会话中。你使用像 jQuery 这样的 javascript 吗? 所以您想在 HTTP 保护伞下发明新的通信模型? ;-) “HTTP 在客户端-服务器计算模型中用作请求-响应协议”en.wikipedia.org/wiki/Http 【参考方案1】:Servlet 应该在没有用户 HTTP 请求的情况下将数据推送给该用户。
这在技术上是不可能的。 HTTP 协议根本不允许这样做。浏览器只有在发送请求时才能从服务器获得响应。一旦完成读取响应,它就不会尝试从其套接字中读取更多内容......直到它发送下一个请求。
您可以做的是在所有页面中添加一些 javascript,这些页面会定期向服务器发出 AJAX 请求,询问是否有要显示的管理消息。
我想你也可以使用 COMET 来做到这一点。
您当前保留最后一个响应并尝试向其写入更多数据的方法失败了,因为它违反了 HTTP 协议和 servlet 状态机。当登录 servlet 完成原始请求时,响应的输出流被刷新到套接字并关闭。它无法重新打开,即使可以,浏览器也不知道如何处理您写入的数据。
更新:对此有一个可行的变体。它涉及WebSockets ...客户端在请求中发送一些特殊的标头,服务器发送101响应和切换协议;见https://en.wikipedia.org/wiki/WebSocket
【讨论】:
Comet 或 websockets 或即将推出的 webRTC dataChannel @ stephen 实际上,我要从 android 应用程序而不是从 android 浏览器或 PC 网络浏览器中访问 URL。有没有其他替代方法可以实现这一目标?通过 android 应用程序,用户将获得连接,管理员会将他们的数据推送给所有时间或某些选定的用户。对于 android,我们有 GCM 谷歌云到设备消息服务,但这将是开销,因为 GCM 只是通知 android 应用程序有新数据在服务器端可用,因此用户必须再次访问服务器才能获取数据。所以我想为什么不将数据从服务器推送到用户。让我知道您对此的看法。 “所以我想为什么不将数据从服务器推送到用户。让我知道你对此的想法。” - 我的想法是 1)我不知道如何你会那样做的。 2) 为什么要为难自己?以上是关于一个从html向servlet中提交数据的问题的主要内容,如果未能解决你的问题,请参考以下文章
提交 HTML 表单时如何将数据从 JSP 传输到 servlet