JSP couldnt link css files and js files - java

I am new to jsp. I am using Glassfish server from netbeans. Problem is that i could link my jsp file with css and javascripts. Here's the format of my file structure
Web Pages
--Web-Inf
--assets
--css
--style.css
--js
--jquery.js
--includes
--header.jsp
--footer.jsp
--sidebar.jsp
--pages
--home.jsp
--index.jsp
Here's the code in my index.jsp
<%#include file="includes/header.jsp" %>
<%
if (request.getParameter("page") == null) {
%>
<%#include file="pages/home.jsp" %>
<%
} else {
%>
<%
}
%>
<%#include file="includes/sidebar.jsp" %>
</div>
<!--Latest end-->
<%#include file="includes/footer.jsp" %>
And here's my code in header.jsp
<link href="${pageContext.request.contextPath}/assets/css/bootstrap.min.css" rel="stylesheet">

You can use relative urls to index.jsp
<link href="assets/css/bootstrap.min.css" rel="stylesheet">

Related

Tomcat caters to multiple user with same memory - JSP

I have a jsp web application, When i try to deploy it to tomcat server, and try to run the application from different machines, i dont get new pages for every user.
My application takes input from html input and keep it in the memory with the press of the button, ao actually i push the values in the memory and keeps it untill reset is pressed.
The problem comes when i goto another machine and run that application, all i get is the same modified page from that previous user.
I have used session management to keep username in the session.
But as if my application is not creating a new session for every new user request.
eg:
main.jsp has some input fields and when i click 'add' those values from html input is stored in memory objects, and is showed in the html inputs till the memory is not cleared.
Now from another machine, i access this application and go to main.jsp, there i get prefilled html input boxes.
why am i not getting a new page everytime i go from a different machine.
Is tomcat server serves all users from same memory space?
Sample code for main.jsp
<%# page import="test.DatabaseAccessConnectionManager" %>
<%# page import="test.FunctionKeywordManager" %>
<%# page import="test.TestScenarioManager" %>
<%# page import="test.DataParameter" %>
<%# page import="test.RepositoryManager" %>
<%# page import="java.util.ListIterator"%>
<%# page import="java.util.*"%>
<%# page import="java.sql.ResultSet"%>
<%# page import="test.TestCase" %>
<%# page import="test.ReportGenerator" %>
<jsp:useBean id="connn" scope="session" class="test.DatabaseAccessConnectionManager"></jsp:useBean>
<jsp:useBean id="connn1" scope="session" class="test.FunctionKeywordManager"></jsp:useBean>
<jsp:useBean id="connn2" scope="session" class="test.TestScenarioManager"></jsp:useBean>
<%
String Username=null;
String projectName = null;
if(session.getAttribute("Username")!=null)
{
Username = session.getAttribute("Username").toString();
projectName = session.getAttribute("ProjectName").toString();
}
else
{
response.sendRedirect("Login.jsp");
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test Scenario Management</title>
<script src="static/js/jquery-2.1.3.min.js"></script>
<script src="static/js/script.js"></script>
<link rel="stylesheet" type="text/css" href="static/css/style.css">
<!-- <script>
function displayMessage(strMessage) {
alert(strMessage);
};
</script> -->
</head>
<body>
<div id="parameterPopup">
<div class="heading">Enter Parameter Values</div>
<br>
<div class="content"></div>
<br>
<div class="buttons">
<input type="button" value="submit values" id="btnSubmitParams" />
</div>
</div>
<div id="wrapper">
<div id="userInfonLogout">
<p>Welcome <b><%=Username %></b></p>
<center><a href="Logout.jsp" >logout</a></center>
</div>
<div id="topheader">
<div id="logo">
<img src="static/images/CS_200px.png"/>
</div>
<center><div id="headertext"><h1>Data Validation Automation</h1></div> </center>
</div>
<div id="navigation">
<ul>
<li>Home</li>
<li>Projects</li>
<li>Repository</li>
<li>Create Test Scenario</li>
<li>Maintain Test Scenarios</li>
<li>Configuration</li>
</ul>
</div>
<div id="body">
<div id="popup" style="display:none">
<div class="content">
</div>
<input type="button" class="close" value="close"/>
</div>
<h2>
<p align="center">
Create Test Scenario</p></h2>
<%!
FunctionKeywordManager objFunctionKeywordManager;
List<String> listKeywords;
TestScenarioManager objTestScenarioManager;
ReportGenerator objReportGenerator=new ReportGenerator();
public void jspInit(){
objFunctionKeywordManager = new FunctionKeywordManager();
listKeywords = objFunctionKeywordManager.getAllFunctionKeyword();
objTestScenarioManager = new TestScenarioManager();
}
%>
<%
RepositoryManager objRM = new RepositoryManager();
List<String> listRepo = objRM.getRepositoryListForProject(projectName);
%>
P.S: A newbie to jsp and tomcat.
Thanks in advance.

Set locale for JSP page in Openfire

I have a plugin for Openfire. There is a JSP page inside it, and I want the locale on this page to be different from the global locale.
I tried this:
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%#taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%
String locale = ParamUtils.getParameter(request, "locale");
%>
<html>
<head>
<title>Page</title>
</head>
<body>
<fmt:setLocale value="<%=locale%>" scope="session"/>
<fmt:message key="hello.world"/> <br>
</body>
</html>
But it doesn't work.
When I switch the global locale, my JSP page shows me right language. But I don't want to change the global locale, I only want to change it for this page.
What can I do to make this work?

Google App Engine: Get source link to entity property

I'm new to Google App Engine so I was hoping that you could help me here.
I'm trying to get the source link to a property of an entity (want to download a json), but can't figure out how.
This is the code:
<%-- //[START all]--%>
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<%# page import="com.google.appengine.api.users.User" %>
<%# page import="com.google.appengine.api.users.UserService" %>
<%# page import="com.google.appengine.api.users.UserServiceFactory" %>
<%-- //[START imports]--%>
<%# page import="com.google.appengine.api.datastore.DatastoreService" %>
<%# page import="com.google.appengine.api.datastore.DatastoreServiceFactory" %>
<%# page import="com.google.appengine.api.datastore.Entity" %>
<%# page import="com.google.appengine.api.datastore.FetchOptions" %>
<%# page import="com.google.appengine.api.datastore.Key" %>
<%# page import="com.google.appengine.api.datastore.KeyFactory" %>
<%# page import="com.google.appengine.api.datastore.Query" %>
<%-- //[END imports]--%>
<%# page import="java.util.List" %>
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<html>
<head>
<link type="text/css" rel="stylesheet" href="/stylesheets/main.css"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Mandatory Assigment 2</title>
</head>
<body>
<h1>Mandatory Assignment 2</h1>
<p>This page shows the CSV files uploaded</p>
<h2>Uploaded CSV files</h2>
<%-- //[START datastore]--%>
<%
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Key guestbookKey = KeyFactory.createKey("Guestbook", "guestbookName");
// Run an ancestor query to ensure we see the most up-to-date
// view of the Greetings belonging to the selected Guestbook.
Query query = new Query("Greeting", guestbookKey).addSort("date", Query.SortDirection.DESCENDING);
List<Entity> greetings = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(5));
if (greetings.isEmpty()) {
%>
<p>There are no CSV files. Please refresh to reload</p>
<%
} else {
%>
<p>CSV files: </p>
<%
for (Entity greeting : greetings) {
pageContext.setAttribute("greeting_content",
greeting.getProperty("content"));
if (greeting.getProperty("user") == null) {
%>
<p>An anonymous person wrote:</p>
<%
} else {
pageContext.setAttribute("greeting_user",
greeting.getProperty("user"));
%>
<p>json string: ${fn:escapeXml(greeting_content)}</a></a></p>
<%
}
%>
<blockquote></blockquote>
<%
}
}
%>
</body>
</html>
<%-- //[END all]--%>
The "${fn:escapeXml(greeting_content)}" code outputs the json as a String, but I would like to be able to download the json instead like this:
<p>download JSON</a></p>
Can someone help me? I'd appreciate it!
Try this code:
<p>download JSON</a></p>
Adding download attribute you can make content downloadable in the format you specified

How to have dynamic css files depending on struts session variable

I have a webpage in which i want the css file to be the same name as a session variable I have set.
For example;
If the session variable was "blue", i want the page to load the CSS file blue.css.
I tried something below which didnt work, and I'm now stuck. My knowledge of struts is very limited.
<LINK rel="stylesheet" type="text/css"
href="<html:rewrite page='/css/<c:out value="${brand}"/>.css'/>">
This is the full code listing at the top of my jsp page
<%# taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%# taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%# taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%# taglib uri="http://jakarta.apache.org/struts/tags-html-el" prefix="html-el"%>
<html:html lang="true">
<head>
<LINK rel="stylesheet" type="text/css" href="<html:rewrite page='/css/${brand}.css'/>">
<html:base/>
I do not know how to find the version of JSP and JSTL I am using. This was a project picked up from someone else and I have never used them before
It isn't working because you put the c:out tag inside the attribute of the html:rewrite tag. Struts tags don't parse when within attribute values.
href="<html:rewrite page="/css/${brand}.css"/>">
according to another user you cant have html rewrite in the attribute but maybe you can do something like below and rewrite the whole link?
<html:rewrite page='<LINK rel="stylesheet" type="text/css" href="/css/${brand}.css"/>'>
Unfortunatily, I couldnt not get any of your answers working. I think it may be a problem with my versions clashing.
For now I have managed to do it by doing;
<%
String brand = (String) session.getAttribute("brand");
if (brand == null || brand.equals("")) {
skin = "standard";
}
%>
<link href="/css/<%=brand%>.css" rel="stylesheet" type="text/css" />
Try this java script, It will work.
variable code is a hidden field in ur jsp.
var code=(document.getElementById('code')).value;
var url;
// Dynamically loads the Style Sheet according to the user logged in
if(code !== null)
{
url="<link rel='stylesheet' type='text/css' href='css/"+code+".css' title='default'>";
}
else
{
url="<link rel='stylesheet' type='text/css' href='css/commonStyle.css' title='default'>";
}
if(url !== null)
{
document.write(url);
}

Do I have to include libs in every tile within Apache Tiles?

I am using Tiles within my web-application. I have a standard-layout (standard.jsp) within the tiles are used. On top of the standard.jsp are a lot of includes, concerning tag-libraries and such.
Let's do a simplified example.
standard.jsp:
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%# include file="/WEB-INF/jsp/includes/include.jsp" %>
<html>
<head>
<tiles:insertAttribute name="head" flush="false"/>
</head>
<body>
<tiles:insertAttribute name="body" flush="false"/>
</body>
</html>
body.jsp:
<div id="body-div">
<p>Hello, <c:out value="${forname}" />!</p>
</div>
This prints:
Hello, !
In the tiles I would like to use the tags, but it's not working. It only works, if I add the includes to the tile-jsp.
body.jsp with includes:
<%# include file="/WEB-INF/jsp/includes/include.jsp" %>
<div id="body-div">
<p>Hello, <c:out value="${forname}" />!</p>
</div>
This prints:
Hello, John!
Is there a better way to do this or do I have to add all includes to every jsp used?
You don't need ALL includes to be present in each of your tiles, but each used tag-library in a tile must specifically be included in the using tile.
eg :
In your example, each tile using the C JSTL library should at least have the <%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> include

Categories