Tomcat caters to multiple user with same memory - JSP - java

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.

Related

Using a form to display model objects in Thymeleaf. Is there a better way to do this?

been trying to get comfortable with Spring Boot/Thymeleaf/frontend development in general. I'm currently using a table on the frontend to display a list of objects from the backend, as well as a form to allow users to view properties of each item in the list.
I feel like the way I'm doing this is really weird and feel that there could be a better way. Would appreciate any tips/feedback, thank you.
HTML/Thymeleaf
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Gambler Card List</title>
<link th:href="#{/css/styles.css}" rel="stylesheet" />
</head>
<body>
<h1>Gambler Card List</h1>
<div class="cardlist-wrapper">
<div class="cardlist-names">
<form action="/gambler/cardlist" method="get">
<table class="cardlist-table">
<tr th:each="card : ${cardList.getCardList()}">
<td>
<input th:class="card-btn"
th:attr="id=${{card.getId()} == {currentCard.getId()} ? 'selected' : ''}"
type="submit" th:value="${card.getName()}" name="cardName">
</td>
</tr>
</table>
</form>
</div>
<div class="cardlist-description">
<h4 th:text="${currentCard.getDescriptionTitle()}"></h4>
<p th:text="${currentCard.getDescription()}"></p>
</div>
</div>
</body>
</html>
Controller
#RequestMapping(value = "gambler/cardlist")
public String baseCardList(Model model,
#RequestParam(value="cardName", required=false) String cardName) {
model.addAttribute("currentCard", cardList.getCardByName(cardName));
model.addAttribute("cardList", cardList);
return "gambler/cardlist";
}
Output (need 10 rep to post image, so here's a link)
One potential improvement/simplification is to remove the form and replace the inputs with anchors. The anchors would look something like this (not tested!):
<a th:href="#{/gambler/cardlist(cardName=${card.name})}">
<button th:text="${card.name}"
th:classappend="${card.id == currentCard.id ? 'selectedButton' : 'deselectedButton'}">Card Name</button>
</a>
You can style selectedButton and deselectedButton as you wish.

passing dynamic value from one jsp page to another using session on button click

I am making a job search web application using java. I have alotted a job id to each job and i am displaying the list of jobs in a bootstrap table from database. on each button click i need to forward this job id to a jsp page where i am adding the applicant name to the database corresponding to the jobs.My code is:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#page import="com.dbutil.CrudOperation"%>
<%# page import="java.util.*,java.sql.*" %>
<%# page import="javax.servlet.http.HttpSession" %>
<html>
<head>
<title>Job List</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/jobs.css">
<script src="jquery.js"></script>
<script src="bootstrap.min.js"></script>
</head>
<body data-spy="scroll" data-target=".navbar" data-offset="50">
<% Connection con = null;
ResultSet rs = null,rs1=null;
PreparedStatement ps = null;
HttpSession hs = null;
hs=request.getSession();
con = CrudOperation.createConnection();
%>
<div class="container">
<%String strsql = "select * from postjob1 where sr>0";
try {
ps = con.prepareStatement(strsql);
rs = ps.executeQuery(); %>
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>Job Id</th><th>Job Category</th><th>Available Posts</th><th>Required Qualifications</th><th>Salary Details</th><th>Location</th><th>Last date of application</th><th></th>
</tr>
</thead>
<% while (rs.next()) {
%>
<tbody>
<tr><td><%=rs.getString("id")%></td><td><%=rs.getString("jobcategory")%></td><td><%=rs.getString("available")%></td><td><%=rs.getString("requiredqualification")%></td><td><%=rs.getString("salarydetails")%></td><td><%=rs.getString("location")%></td><td><%=rs.getString("lastdate")%></td><td><button type="button" class="btn btn-primary" onclick="<% hs.setAttribute("ID",rs.getString("id")); %>" >View Details </button></td></tr>
</tbody>
<%}
} catch (SQLException se) {
System.out.println(se);
}%>
</table>
</div
</div>
</body>
</html>
I am facing difficulty when I am retreiving the session value ID in other jsp page as i am getting id of the last row in the table irrespective of whichever button i click.Kindly help me run my code.Thanks!
<% hs.setAttribute("ID", rs.getString("id")); %>
In the excerpt above, you are setting the session attribute "ID". After executing the while loop, it stores the last "id" value. The same value is recovered in the destination page, whichever the clicked link.
A possible solution is passing the id in the invoked URL to the following page.
<a href="jobDetails.jsp?idJob=<%=rs.getString("id")%>">
<button type="button" class="btn btn-primary">View Details</button>
You can recover the parameter in jobDetails.jsp using
request.getParameter("idJob")
Note: I'm a bit rusty in JSP. Maybe I've forgotten a quote or something, but that's the idea.
You can pass job id like request's parameter.
For example you can call your jsp-page like that:
<a href="jobDetails.jsp?job_id=3" />
Where job_id is a parameter name and 3 is its value.
After that you can read parameter's value in your jobDetails.jsp in next way:
<%=request.getParameter("job_id")%>

Class attribute on selected <li> on a JSP tag

I have the following JSP tag side_menu.tag:
<%# tag language="java" pageEncoding="UTF-8"%>
<div class="wrap">
<ul class="main_menu">
<li>X</li>
<li>Y</li>
<li>Z</li>
</ul>
</div>
This tag is used in my wrapper tag, aptly named wrapper.tag:
<html>
<head>
<title>Home</title>
<link rel="stylesheet" type="text/css" href="STYLE/main.css">
</head>
<body>
<t:main_banner/>
<t:side_menu/>
<jsp:doBody/>
</body>
</html>
Thing is, one of the <li> should have a class="selected" which reflects a styling that makes it different than the rest based on the current action, is there a way to make this dynamic? JSTL perhaps? Or am I tagging and compressing it too much? I'm having trouble coming up with a solution for this other than just eliminating the side_menu.tag and repeat the same <ul> in every .jsp where I need it.
Thanks for every answer!

Including JSP file within another JSP file

I'm writing a wizard for creating a user in my application with Spring MVC. At each step the controller will set session attributes for the completed wizard fields.
I want the wizard to look the same regardless of which page it's on, except for each page's fields, obviously. For example, menus and links at the top of the page and buttons at the bottom should remain the same.
I have the following JSP
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Create a new User</title>
</head>
<body>
<h1>User Creation Wizard</h1>
Step <c:out value = "${pageNum}"/>/<c:out value = "${pageMax}"/>
<form action="" method="POST">
<jsp:include page="userform${pageView}.jsp"/>
<input name = "currentPage" type = "hidden" value = "${pageNum}"/>
<c:if test = "${pageNum > 1}">
<input name = "prev" type = "submit" value = "Previous" />
</c:if>
<c:if test = "${pageNum < pageMax}">
<input name = "next" type = "submit" value = "Next" />
</c:if>
<c:if test = "${pageNum == pageMax}">
<input name = "submit" type = "submit" value = "Finish" />
</c:if>
</form>
</body>
</html>
In the jsp I'm including, do I need to remove the <html>, <head>, and <body> tags? The above code is based on this example.
Yes, you'd need to remove the <html>, <head> and <body> tags from the included JSP file. As they'd already be present in the including file keeping them would result in invalid HTML.
Only the content that you want to vary would be in the JSP file you're including. Everything else, including the necessary <html>, <head> and <body> tags, would be in the JSP file that does the including.

text values to be entered in jsp for registration form

I am doing one small requirement on servlet and jsp.
servlet will contain variables id,name,email,gender. some times the values will be null.
Some times the values of the varaible are null. For example id and name are containing the values 1123 and pratap.
response.setContentType("text/html;charset=UTF-8");
try {
//TODO output your page here
RequestDispatcher view = request.getRequestDispatcher("registration.jsp");
view.forward(request, response);
request.setAttribute("id","value");
} finally {
}
my jsp page
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form method="GET" action='registration1'>
<input type="text" name="address"/>
<input type="text" name="phoneno"/>
<input type="text" name="pincode" />
<label>${id}</label>
<input type="submit"/>
</form>
</body>
</html>
so that the control is going to registration.jsp
In registration.jsp i should get the text boxes for email and gender and for the id and name i should get the values in the text boxes such that the user can not change those values.(because those are already filled and proved to be correct.)
for the above jsp i tried with id but i am unable to see the id value in jsp.
how to pass those varaibles to the text boxes of the jsp and making the prompt to enter the values if the value is null.
Thank you..
You need set values as request attributes in Servlet and get them in JSP.
After getting them in JSP, Check accordingly and enable /disable the form controls.
Servlet:
request.setAttribute("phoneno","9998386033");
JSP:
<%
String phoneno=null;
if(request.getAttribute("phoneno")!=null)
phoneno = request.getAttribute("phoneno").toString();
%>
<% if(phoneno!=null) {
out.println("<INPUT TYPE=\"text\" name=\"phoneno\" value=\""+phoneno+"\" disabled=\"disabled\" ");
} else {
out.println("<INPUT TYPE=\"text\" name=\"phoneno\" ");
}
%>
FOR JSP EL
<c:if test="${empty phoneno}">
<INPUT TYPE="text" name="phoneno" value="${phoneno}" disabled="disabled"/>
</c:if>
<c:if test="${not empty phoneno}">
<INPUT TYPE="text" name="phoneno"/>
</c:if>

Categories