掲示板

Where to store some data use by a protlet?

thumbnail
12年前 に Adi florescu によって更新されました。

Where to store some data use by a protlet?

New Member 投稿: 19 参加年月日: 11/09/23 最新の投稿
To make my point simple I attached a file with what I already tried and works, but I don't think it's the best way to do thinks.

So basically, in a portlet I want to remember the number of times a user clicks on a button while he/she is logged in. If he/she logs out then the counter is set to "0". I did that by creating a servlet (which doesn't solve all the problems), and I would like to know how would you do it.


For reference, here's the .jsp code. Suggestions are welcomed.
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<portlet:defineobjects />

<h1>Inside somany.jsp</h1>

<div>This is a play with storing values on the server side.</div>
<div>
	<input id="<portlet:namespace />value" type="text" value="Not initialized!">
</div>
<div>
	<button onclick="decrease();">---</button>
	<button onclick="set();">Set a number value at 10</button>
	<button onclick="increase();">+++</button>
</div>
<!-- <script type="text/javascript" src="someScript.js"></script> -->
<script>
	var g_nSomeNumber;
	getValue();

	// adding function
	function increase() {
		g_nSomeNumber++;
		update();
	}//end of increase

	// setting funtion
	function set() {
		alert("now the number is " + g_nSomeNumber
				+ " and it will be reseted to 10");
		g_nSomeNumber = 10;
		update();
	}

	// substracting function
	function decrease() {
		g_nSomeNumber--;
		update();
	}//end of decrease

	//show the information on screen
	function update() {
		document.getElementById("<portlet:namespace />value").setAttribute(
				"value", g_nSomeNumber);
		setValue(g_nSomeNumber);
	}

	//get the value from the servlet
	function getValue() {
		var ajax = XMLHttpRequest();
		ajax.open("GET",
				"http://ws805-is:8080/EasyPeasy-portlet/StoreData?type=get");
		ajax.send(null);
		ajax.onreadystatechange = function() {
			if (ajax.readyState == 4) {
				if (ajax.status == 200) {
					document.getElementById("print").innerHTML = ajax.responseText;
					g_nSomeNumber = parseInt(ajax.responseText);
				}
			}
		};
	}

	//set the value in the servlet
	function setValue(value) {
		var ajax = XMLHttpRequest();
		ajax.open("GET",
				"http://ws805-is:8080/EasyPeasy-portlet/StoreData?type=set&value="
						+ value.toString());
		ajax.send(null);
		ajax.onreadystatechange = function() {
			if (ajax.readyState == 4) {
				if (ajax.status == 200) {
					document.getElementById("print").innerHTML = ajax.responseText;
				}
			}
		};
	}
</script>


&lt;%
	renderResponse.addProperty("val", "is set");
	renderResponse.setProperty("val", "nothing");
	renderRequest.setAttribute("val", "set!");
	String valReq = renderRequest.getParameter("val");
%&gt;
&lt;%=valReq%&gt;

<div id="print">Print something here!</div>

And the servlet code:
package com.test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class StoreData
 */
public class StoreData extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private String someString;
    private String value;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StoreData() {
        super();
        someString = "a value was added in the constructor";
        value = "1000";
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String type = request.getParameter("type");
		if (type != null &amp;&amp; type.charAt(0) == 's') {
			someString = "set!";
			value = request.getParameter("value");
			response.addHeader("value", value);
		}
		else {
			someString = "get!";
			PrintWriter out = response.getWriter();
			out.print(value);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}


And here on the bottom, the picture of the portlet with a firebug window:

添付ファイル:

thumbnail
12年前 に David H Nebinger によって更新されました。

RE: Where to store some data use by a protlet?

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
Adi florescu:
So basically, in a portlet I want to remember the number of times a user clicks on a button while he/she is logged in. If he/she logs out then the counter is set to "0". I did that by creating a servlet (which doesn't solve all the problems), and I would like to know how would you do it.


Very simple and not real world. If these requirements were given to me, I'd store the count in the portlet session. it is unique for each instance/user and is destroyed upon user logout.
thumbnail
12年前 に Adi florescu によって更新されました。

RE: Where to store some data use by a protlet?

New Member 投稿: 19 参加年月日: 11/09/23 最新の投稿
Thanks David!

That's actually a good idea. I have a follow up question, how about if you have other portlets that need to access the same variable, where should I store it?
thumbnail
12年前 に David H Nebinger によって更新されました。

RE: Where to store some data use by a protlet?

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
They shouldn't share it. You should use some form of IPC to notify the interested portlets that the count has been incremented.