I have to submit some links via the control panel of a website and they should automatically appear in the jsp page,next time the user refreshes it.To do this,I click the submit button after pasting the link,a post request is generated and servlet handles that link by calling a bean's function from itself and stores the link there in the ArrayList.
我必须通过网站的控制面板提交一些链接,它们应该自动出现在jsp页面中,下次用户刷新它。为此,我在粘贴链接后点击提交按钮,生成一个帖子请求, servlet通过从自身调用bean的函数来处理链接,并将链接存储在ArrayList中。
snippet from servlet
来自servlet的片段
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String link = request.getParameter("song link");
StoreSongLink store = new StoreSongLink(); // A Bean class
int ret = store.storeSongLink(link); // calls the bean method
// if ret is 0,return to the cpanel home page
Bean class
public class StoreSongLink {
public static ArrayList<String> linkList = new ArrayList<String>();
public int storeSongLink(String link) {
linkList.add(link);
return 0;
}
}
After this I try to fetch the submitted links as (in the jsp page) :
在此之后,我尝试获取提交的链接(在jsp页面中):
<%! ArrayList<String> songList = new ArrayList<String>(); %>
<%
songList = StoreSongLink.linkList;
%>
<% for (String links : songList){ %>
<li><%= links%></li>
<%}%>
I see the link submitted one or two time and then it gets vanished after I refresh the same page and I see the message that isn't any link submitted yet! Why is this so ? I know this may not be the best of approach but still I want to do this way.
我看到链接提交了一两次,然后在刷新同一页面后它就消失了,我看到的消息还没有提交任何链接!为什么会这样?我知道这可能不是最好的方法,但我仍然想这样做。
Why don't i see the link submitted after I refresh the page ? How is the data lost from the ArrayList ?
刷新页面后,为什么我看不到提交的链接?如何从ArrayList中丢失数据?
3 个解决方案
#1
0
The StoreSongLink object that you create in the servlet has its scope limited to that particular invocation. Where are you storing this object, or its contents? You need to put it either in the session or in a database, depending on the functionality that you expect.
您在servlet中创建的StoreSongLink对象的范围仅限于该特定调用。你在哪里存储这个对象或其内容?您需要将它放在会话中或数据库中,具体取决于您期望的功能。
#2
0
Looks like you want to instantiate that SongStoreLink once per session. Before creating the object you should retrieve it from the session, if one is found use it otherwise create the object ans save it in the session.
看起来您希望每个会话实例化一次SongStoreLink。在创建对象之前,您应该从会话中检索它,如果找到一个,则使用它,否则创建对象并将其保存在会话中。
#3
0
Get the StoreSongLink instance from a session or something similar.
从会话或类似的东西获取StoreSongLink实例。
StoreSongLink store = (StoreSongLink)request.getSession().getAttribute("storeSongLink");
if (store == null)
{
store = new StoreSongLink();
request.getSession().setAttribute("storeSongLink", store);
}
Alternatively you could use a singleton pattern to create/get the StoreSongLink instance. Furthermore it would be wise refactoring the storeSongLink method and avoid scriptlets in JSP. There are plenty of frameworks which seperate code and layout.
或者,您可以使用单例模式来创建/获取StoreSongLink实例。此外,重要的是重构storeSongLink方法并避免JSP中的scriptlet。有很多框架可以分离代码和布局。
#1
0
The StoreSongLink object that you create in the servlet has its scope limited to that particular invocation. Where are you storing this object, or its contents? You need to put it either in the session or in a database, depending on the functionality that you expect.
您在servlet中创建的StoreSongLink对象的范围仅限于该特定调用。你在哪里存储这个对象或其内容?您需要将它放在会话中或数据库中,具体取决于您期望的功能。
#2
0
Looks like you want to instantiate that SongStoreLink once per session. Before creating the object you should retrieve it from the session, if one is found use it otherwise create the object ans save it in the session.
看起来您希望每个会话实例化一次SongStoreLink。在创建对象之前,您应该从会话中检索它,如果找到一个,则使用它,否则创建对象并将其保存在会话中。
#3
0
Get the StoreSongLink instance from a session or something similar.
从会话或类似的东西获取StoreSongLink实例。
StoreSongLink store = (StoreSongLink)request.getSession().getAttribute("storeSongLink");
if (store == null)
{
store = new StoreSongLink();
request.getSession().setAttribute("storeSongLink", store);
}
Alternatively you could use a singleton pattern to create/get the StoreSongLink instance. Furthermore it would be wise refactoring the storeSongLink method and avoid scriptlets in JSP. There are plenty of frameworks which seperate code and layout.
或者,您可以使用单例模式来创建/获取StoreSongLink实例。此外,重要的是重构storeSongLink方法并避免JSP中的scriptlet。有很多框架可以分离代码和布局。