[jboss-user] [JBoss Portal] - Re: How to get started?? Which jars for needed for simple po
nadley
do-not-reply at jboss.com
Sun May 3 13:23:51 EDT 2009
Well, you're right - I never declared that variable because I thought it should be available for every portlet. Isn't it possible to include a certain jar so that I can access "portletSession"? I've seen "portletSession" in various sample codes...
Here is my JSP:
| <%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
|
| <portlet:defineObjects/>
|
| <div align="center">
| <%!
| java.util.Random random = new java.util.Random();
| int toGuess;
| int guessedNumber;
| long start;
| long end;
| long minuten;
| long sekunden;
| %>
| <%
| String value = renderRequest.getParameter("guessedNumber");
|
| java.util.List <String> list = (java.util.List <String>)portletSession.getAttribute("bereitsGeraten");
| if (portletSession.getAttribute("toGuess")!=null){
| toGuess = (Integer) portletSession.getAttribute("toGuess");
| }else {
| toGuess = random.nextInt(1000);
| }
| if (value != null){
| if (value.equals("neustart")){
| list = new java.util.ArrayList <String> ();
| guessedNumber = -1;
| toGuess = random.nextInt(1000);
| }
| else {
| if (list == null){
| list = new java.util.ArrayList<String>();
| }
| list.add(value);
| try {
| guessedNumber = Integer.parseInt(value);
| }catch (Exception e){
| out.print("Fehler beim Lesen der eingegebenen Zahl!");
| }
| }
| }
|
| portletSession.setAttribute("bereitsGeraten",list);
| portletSession.setAttribute("toGuess",toGuess);
| %>
| <form action="<portlet:actionURL><portlet:param name="page" value="mainview"/></portlet:actionURL>" method="POST">
|
| <% if (guessedNumber == toGuess){
| end = new java.util.Date().getTime();
| minuten = (end-start)/(60*1000);
| sekunden = ((end-start)%(60*1000))/ 1000;
| %>
| Super! Das war die gesuchte Zahl!!! <br>
| Sie haben das Ergebnis in <%=minuten%> Minuten und <%=sekunden%>
| Sekunden beim <%= list.size()%>. Versuch gefunden! <br>
| <input type=hidden name=guessedNumber value="neustart">
| <input type=submit value=Neustart>
| <% }else {
| if ((value == null)||(value.equals("neustart"))) {
| start = new java.util.Date().getTime();%>
| Hallo! <br> Bitte eine Zahl zwischen 0 und 1000 erraten!
| <% } else if (guessedNumber > toGuess) { %>
| Die zu erratende Zahl ist kleiner! Noch ein Versuch:
| <% } else if (guessedNumber < toGuess) { %>
| Die zu erratende Zahl ist gröÃer! Noch ein Versuch:
| <% } %>
| <br>
| <input type="text" name="guessedNumber">
| <input type="submit" value="raten">
| <br><br>
| Bereits geratene Zahlen: <%=list %>
| <%} %>
|
| </form>
| </div>
|
It's a little game to guess a random number and it worked fine as a jsp-only application.
View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4228175#4228175
Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4228175
More information about the jboss-user
mailing list