How to include HTML in JSP? - java

I've searched for this question, there are some answers, but not exactly as my question. So, here is my jsp code:
<body>
<%
if (manager.isValid(request.getParameter("username"),request.getParameter("password"))){
out.print("<h1> Welcome ");
out.print(request.getParameter("username") + "</h1>");
} else {
out.print("<h1> Please Try Again </h1> <br />");
out.print("Either your username or password is incorrect. Please Try again <br /> <br />");
}
%>
<%# include file="LoginForm.html" %>
but instead of this, I want to include "loginForm.html" only in "else" block.
How can I do it?
Of course this doesn't work, but you'll guess what I want:
<%
if (manager.isValid(request.getParameter("username"),request.getParameter("password"))){
out.print("<h1> Welcome ");
out.print(request.getParameter("username") + "</h1>");
} else {
out.print("<h1> Please Try Again </h1> <br />");
out.print("Either your username or password is incorrect. Please Try again <br /> <br />");
include file="LoginForm.html" ;
}
%>

I would like to show you a way, try like below. You can try with else instead with if.
<body>
<%
int x = 10;
if(x>10) {
%>
<%#include file="some.html" %>
<%
}
%>
</body>

You could also include reusable html in a jsp file function and call it for printing where needed. e.g.
config.jsp contains
<%!
public void printMenu()
{
%>
HTML HERE
<%!
}
%>
home.jsp contains
<%#include file="config.jsp"%>
<!DOCTYPE html>
<html>
<body>
<% printMenu(); %>
<div id="PeopleTableContainer" style="width: 800px;"></div>

Related

Using template in Underscore JAVA

How to use underscore java template library for using template in java.
Library : https://github.com/javadev/underscore-java
Sample Template:
<h2>
<%- listTitle %>
</h2>
<ul>
<% _.each( listItems, function( listItem ){ %>
<li>
<%- listItem.name %>
<% if ( listItem.hasOlympicGold ){ %>
<em>*</em>
<% } %>
</li>
<% }); %>
</ul>
<% var showFootnote = _.any(
_.pluck( listItems, "hasOlympicGold" )
); %>
<% if ( showFootnote ){ %>
<p style="font-size: 12px ;">
<em>* Olympic gold medalist</em>
</p>
<% } %>
How can populate this template in java.
Thanks.

Identifier expected after this token

I got a error : Identifier expected after this token
Syntax error on token "tag", Identifier expected after this token
And here is my code:
<%# page import="photoshare.Picture" %>
<%# page import="photoshare.PictureDao" %>
<%# page import="photoshare.HREF_AND_IMGSRC" %>
<%# page import="photoshare.ToolsDao" %>
<%# page import="org.apache.commons.fileupload.FileUploadException" %>
<%# page import="java.util.ArrayList" %>
<%# page import="java.util.List" %>
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Get POP Tags</title></head>
<body>
<h1>With Photos with popular Tags:</h1>
<%
String tagName = request.getParameter("tag");
List<String> popTags = new ArrayList<String>();
ToolsDao toolsDao = new ToolsDao();
popTags = toolsDao.getPopularTags();
for (tag:popTags) {%>
<a href="popTags.jsp?tag=<%= tagName %>">tagName <a>
<%}
List<HREF_AND_IMGSRC> tagPhotos = toolsDao.getHrefAndImgSrcFromTag(tagName);
for (HREF_AND_IMGSRC photo:tagPhotos) {
int pictureId = photo.getPictureID();
int selectedAlbumID = photo.getAlbumID();
String selectedAlbum_id = Integer.toString(selectedAlbumID);
%>
<td><a href="picture.jsp?id=<%= pictureId %>&album_id=<%= selectedAlbum_id%>">
<img src="/photoshare/img?t=1&picture_id=<%= pictureId %>"/></a></td>
<%
}
%>
Click here to Main Page<br>
</body>
</html>
I am finding some reasons for this but nothing similar, can anybody help me to figure out what is the problem for this specified error?
I found the problem which is :
for (tag:popTags) {%>
<a href="popTags.jsp?tag=<%= tagName %>">tagName <a>
<%}
I have to declare the type of the tag in popTags list such as:
for (String tag:popTags) {%>
<a href="popTags.jsp?tag=<%= tagName %>">tagName <a>
<%}

How to make a youtube link as embed?

<%
String song = request.getParameter("songname");
String band = request.getParameter("band");
String url = request.getParameter("url");
%>
<div align="center">
<br><br> <% out.print(band + " - " + song); %><br><br>
<iframe width="560" height="315" src="<%out.print(url);%>" frameborder="0" allowfullscreen></iframe>
</div>
<%
%>
Let's say url i get is like this https://www.youtube.com/watch?v=kXYiU_JCYtU i want to make it look like https://www.youtube.com/embed/kXYiU_JCYtU this so i can put it at src=""
You can use the replace() method:
<% out.print(url.replace("watch?v=", "embed/")); %>

Number guesser game generates a different numbers each guess

I have a Java Server Page that lets the user pick a number of their choice from 1-1000. The page then uses the number entered and finds out if the number matches the number that is generated. -- Pictures below if ^ is unclear. Currently, the program generates a different number each time the user guesses a number -- whether it is correct or not. How do I make it so that the program only generates a number when the user either refreshes the page or guesses correctly?
JSP code:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# page import = "Chapter12.RandomGuess" %>
<jsp:useBean id = "randomGuessId" class = "Chapter12.RandomGuess" scope = "page" >
</jsp:useBean>
<jsp:setProperty name = "randomGuessId" property = "*" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Guess Random Number</title>
</head>
<body>
<h3>Guess a number from 1 to 1000</h3>
<form method = "post">
Enter guess: <input name = "guess" /><br /><br />
<input type = "submit" name = "Submit" value = "Take Guess" />
<input type = "reset" value = "Reset" /><br /><br />
Your guess of <jsp:getProperty name = "randomGuessId" property="guess" />
is <%= RandomGuess.guess(randomGuessId.getAnswer()) %>
</form>
</body>
</html>
Java code:
import java.util.Random;
public class RandomGuess {
private int guess;
Random r = new Random();
private int low = 1;
private int high = 1000;
int R = r.nextInt(high-low) + low;
public int getGuess() {
return guess;
}
public void setGuess(int newValue) {
guess = newValue;
}
public String getAnswer() {
String tooLow = "too low.";
String tooHigh = "too high.";
String correct = "correct!";
if(guess == R)
return correct;
else if(guess < R)
return tooLow;
else
return tooHigh;
}
public static String guess(String s) {
return s;
}
}
picture: http://i.imgur.com/dMSZ7SD.png
Every time the page refreshes, a new instance of the bean is created - with a new number.
To preserve the number across calls, use a static field in your class.
Better yet, use JavaScript!
Find complete program here:http://sickprogrammersarea.blogspot.in/2014/01/creating-number-guesser-in-jsp_8296.html
Hello friends this is a simple program of creating a number guesser in JSP....Have a look...
Number.jsp:
<html>
<head><title>Number Guesser</title></head>
<body>
<% int num=(int)(Math.random()*100); %>
<h2>Welcome To Number Guesser</h2>
<br><h3>Want To Check Your Guessing Power.....GIVE IT A TRY?????</h3>
<form name="guess" action="try.jsp?c=0" method="post">
<input type="text" name="val" value="<%=num %>" hidden>
<input type="submit" value="GO">
</form>
</body>
</html>
try.jsp :
<html>
<head><title>Number Guesser</title></head>
<body>
<% String str=request.getParameter("val");
boolean flag=true;
int num=Integer.parseInt(str);
int c=Integer.parseInt(request.getParameter("c"));
if(c!=0)
{
int guess=Integer.parseInt(request.getParameter("guess"));
if(num==guess){
flag=false;
%>
<h3>Congratulation...You Successes After <%=c%> attempts</h3>
<b>Want to Improve...try again</b>
<% }else if(num>guess) { %>
<h3>You Guessed Lower...Try Bigger Number</h3>
<% }else{ %>
<h3>You Guessed Higher...Try Smaller Number</h3>
<% }
}
if (flag) { c++; %>
<h3>I Guessed a Number between 1 to 100. Try To Guess..</h3>
<form name="guess" action="try.jsp?c=<%= c%>" method="post">
<input type="text" value="<%=num %>" name="val" hidden>
Make Your Guess : <input type="text" name="guess" size=10 maxlength=3>
<input type="submit" value="GO">
</form>
<% } %>
</body>
</html>
You could pass the user number back as a query parameter.
I'm guessing you are not too familiar with the MVC pattern or Spring, but there are plenty of resources available to get you quickly up and running. I'll try to make a strip down demo later, if I remember.

Java Facelets and session?

How can I use session for facelets ?
What's the syntax...?
I would put a code like this
<% String loginSession = (String)session.getAttribute("login"); %>
<% if(loginSession != null){ %>
Welcome <%= session.getAttribute("firstName") %> !
<% }else{ %>
Guest
<% } %>
Thanks
#{sessionScope.login}
You can't have if-s in JSF (you can with JSTL, but it has complications). Instead you can choose to render or not a component:
<h:outputText value="Guest" rendered="#{sessionScope.login != null}" />

Categories