Java 从我的JSP中的哈希映射检索值
我试图实现的一些背景信息是,用户点击JSP页面上的提交按钮,它需要将提交的消息发送到文本文件,然后我需要访问该文件并检索JSP页面中文件中的所有消息。请帮助我,我已经花了太长时间,我不知道我需要做什么才能迭代哈希映射来显示所有消息 这就是我的代码现在的样子: 控制器:Java 从我的JSP中的哈希映射检索值,java,jsp,servlets,Java,Jsp,Servlets,我试图实现的一些背景信息是,用户点击JSP页面上的提交按钮,它需要将提交的消息发送到文本文件,然后我需要访问该文件并检索JSP页面中文件中的所有消息。请帮助我,我已经花了太长时间,我不知道我需要做什么才能迭代哈希映射来显示所有消息 这就是我的代码现在的样子: 控制器: public class TwitServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request,
public class TwitServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String twits = "";
String path = getServletContext().getRealPath("/WEB-INF/twit.txt");
HttpSession session = request.getSession();
String tweet = request.getParameter("tweet");
System.out.print(tweet);
String usern = (String) session.getAttribute("user");
String alias = (String) session.getAttribute("uname");
twitDB.insert(tweet, usern, alias, path);
Map test = twitDB.getTwit("/Users/emilio/Desktop/twit.txt");
session.setAttribute("test",test);
getServletContext()
.getRequestDispatcher("/home.jsp").forward(request, response);
}
}
数据库(目前使用txtfile):
public类twitDB{
公共静态void insert(字符串twit、字符串用户、字符串uname、字符串路径)引发IOException{
日期=新日期();
SimpleDataFormat ft=新的SimpleDataFormat(“yyyy/MM/dd”);
字符串dateString=ft.format(日期);
File File=新文件(“/Users/emilio/Desktop/twit.txt”);
try(PrintWriter out=new PrintWriter(new FileWriter(file,true))){
out.println(“[@”+uname+”]:“+”+dateString);
out.println(用户);
out.println(twit);
out.println(“.”);
out.close();
}
捕获(iox异常){
//破例做事
iox.printStackTrace();
}
}
公共静态映射getTwit(字符串文件名)引发IOException{
Map tweets=newhashmap();
File File=新文件(“/Users/emilio/Desktop/twit.txt”);
扫描仪输入=新扫描仪(文件);
while(在.hasNextLine()中){
字符串uname=in.nextLine();
字符串名称=in.nextLine();
字符串twit=in.nextLine();
字符串填充符=in.nextLine();
Tweet Tweet=新Tweet(uname、name、twit);
tweets.put(uname,tweet);
tweets.put(名称,tweet);
tweets.put(twit,tweet);
}
in.close();
返回推文;
}
}
我的JSP的一部分:
<button type="submit" method="post" class="btn btn-twitter">
<span class="glyphicon glyphicon-pencil"/>Tweet
</button>
</div>
</form>
</div>
</div>
<div class = "row feed">
<p>
<c:forEach items="${test}" var="test">
</c:forEach>
</p>
推特
如果确实从服务器获取数据,则缺少要打印的标记
<c:forEach items="${test}" var="test">
<c:out value="${test}"/>
</c:forEach>
如果确实从服务器获取数据,则缺少要打印的标记
<c:forEach items="${test}" var="test">
<c:out value="${test}"/>
</c:forEach>
感谢您的输入。出于某种原因,它会这样显示:tweet to test=business。Tweet@f01f5e7tweet=业务。Tweet@172f4335再次推特=业务。Tweet@3e449c13Hiii=业务。Tweet@6f807472知道为什么会这样吗?谢谢你的意见。出于某种原因,它会这样显示:tweet to test=business。Tweet@f01f5e7tweet=业务。Tweet@172f4335再次推特=业务。Tweet@3e449c13Hiii=业务。Tweet@6f807472你知道为什么会这样吗。