calling jsp file from servlet - java

i have three servlet. In first servlet i have Request Dispatcher so it is redirect to my Dynamic List.jsp file. The jsp file contains
<div class='feedItem'> <c:out value="${feed.tagRuleType}"> </c:out> </div>
<div class='feedItem'><input type="submit" value="Update" onClick="updateRows(${feed.configId})"></div>
<div class='feedItem'><input type="submit" value="Delete" onClick="deleteRows(${feed.feedId})"></div>
When i click on the update button i can able to call the servlet through javascript. Inside servlet i am using Request Dispatcher:
RequestDispatcher dispatcher = request.getRequestDispatcher("/FeedUpdate.jsp");
dispatcher.forward(request, response);
But it is not redirecting to FeedUpdate.jsp file
It is iterating the list object. Inside the tag i added update button. so when i click update button corresponding row id i will get for updating a record.
When i click the button i can able to call servlet and the get the id. when i redirect to the jsp page using Request Dispatcher inside the servlet it is not redirecting to jsp page.
Can anyone the give the suggestion why it is not redirecting to jsp file.

if you have used Jquery Ajax call to call your servlet, your dispatcher forward won't work. You may want do the redirect in the success method of Ajax:
jQuery.ajax({
type:"POST",
url : "servlet",
data : param,
success : function(data) {
windows.location.href='/myservlet?id='+data;
});

So basically you want to call other method in another JSP and takes id as the parameter, if yes, then you can do it this way :
<h3>Sections List</h3>
<c:if test="${!empty listSections}">
<table class="tg">
<tr>
<th width="80">Section ID</th>
<th width="120">Section name</th>
<th width="120">Section size</th>
<th width="120">Section position</th>
<th width="60">Edit</th>
<th width="60">Delete</th>
</tr>
<c:forEach items="${listSections}" var="section">
<tr>
<td>${section.sectionid}</td>
<td>${section.sectionname}</td>
<td>${section.sectionsize}</td>
<td>${section.sectionposition}</td>
<td><a href="<c:url value='/editsection/${section.sectionid}' />" >Edit</a></td>
<td><a href="<c:url value='/removesection/${section.sectionid}' />" >Delete</a></td>
</tr>
</c:forEach>
</table>
</c:if>
The method editSetion and RemoveSection can be in different JSP files as well, depends how you have mapped it. Is this what you are looking for? I cannot understand exactly what you want, as you have not provided much information, no code too.. Please add more code and properly explain if this is not what you expected.

Related

Jsp form submit with list to spring MVC controller

I am new to jsp and spring MVC.
I had a jsp which display students list to take attendance with a checkbox in the form.
The Students jsp contains student details from student table and checkbox is from attendance table and my jsp page is like this
Jsp Code
<tr>
<th width="80">Student No</th>
<th width="120">Student Name</th>
<th width="120">Father Name</th>
<th width="60">Mobile No</th>
<th width="60">Present</th>
</tr>
<c:if test="${!empty studentsList}">
<c:forEach items="${studentsList}" var="student">
<tr>
<td>${student.studentId}</td>
<td>${student.studentName}</td>
<td>${student.fatherName}</td>
<td>${student.mobileNo}</td>
<td><form:checkbox path="attendance.presentFlag" /></td>
</c:forEach>
</c:if>
</table>
<div align="center">
<tr>
<td></td>
<td><input type="submit" value="Present" style="margin-top: 12px;"/></td>
</tr>
</div>
</form:form>
Above jsp will display list of students with a checkbox.
Now I want to submit the modified list in jsp to spring MVC controller to store in database based on the checkbox checked flag, can any one please suggest me how to overcome jsp form submission to spring mvc, I am unable to get what to do. I am able to call a method of controller what I have do for list submission.
Create a button instead of submit and call a java script function and then submit the form after applying your logic.
You need few things inside form tag:
method="GET/POST"
action="${request.contextPath}/requestMapping"
modelAttribute="attendance"
<form:checkbox path="presentFlag" /> -> will autobind value if attendance object have array of booleans "presentFlag"
Submit name and value determine request parameter with name and value respectively.
Your Spring controller should have request mapping same as in action, as well as request parameter and model attribute "attendance".
https://www.mkyong.com/spring-mvc/spring-mvc-checkbox-and-checkboxes-example/

Print only html table using javascript

I have the following basic html table with a Java function to print only the table content However the Print button does nothing when clicked,I figure probably the java coding is the issue, however it is not working could I get a third pair of eyes to assist please
<html>
<body>
<table border="1" cellpadding="3" id="printTable">
<tbody><tr>
<th>First Name</th>
<th>Last Name</th>
<th>Points</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
<tr>
<td>Adam</td>
<td>Johnson</td>
<td>67</td>
</tr>
</tbody></table>
<br />
<br />
<button>Print me</button>
<script>
function printData()
{
var divToPrint=document.getElementById("printTable");
newWin= window.open("");
newWin.document.write(divToPrint.outerHTML);
newWin.print();
newWin.close();
}
$('button').on('click',function(){
printData();
})
</script>
</body>
</html>
When working with javascript, it is often useful to look at your browser's console. After opening this page, mine has the error:
ReferenceError: $ is not defined
$ is a global variable inserted by jQuery. Try adding
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
to the code. This will include the jQuery library.
The call to the javascript function is not correct. When you're using the '$' symbol it means that you're using JQuery. You have 2 options:
1 - Include the jquery library to your page:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
2 - Call the javascript function directly on the button on the 'onclick' event. This is the best option in your case:
<button onclick="printData()">Print me</button>
Change this var divToPrint=document.getElementById("printTable");
to this var divToPrint=document.getElementById("<%=printTable.ClientID>");
and add jquery library <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

How to call j_security_check from within a servlet deployed on weblogic server

My HTML LOGIN Page
<html>
<head>
<title>FormBased Authentication Demo in WebLogic Sample</title>
</head>
<body bgcolor=maroon text=white>
<center>
<h2>Please Enter Your UserName & Password (FormBased Auth Example)</h2>
<form method="POST" action="j_security_check">
<table border=5%>
<tr>
<td>Username:</td>
<td><input type="text" name="j_username"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="j_password"></td>
</tr>
<tr>
<td colspan=2 align=right><input type=submit value="Submit"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
j_security_check is like a servlet provided by weblogic for implementing SQLAuthencticatoin.
Now I want to call the j_security_check servlet from within a servlet(my own custom servlet) as I need to execute some more code before routing it to j_security_check. Can anyone help me out ?
j_security_check is used in Form-Based Authentication. So you can just do a POST to the j_security_check with "j_username" and "j_password". Typically this is done on a JSP, but it could be done within a servlet as well. This part probably isn't news, but as it is a little unclear what your particular issue is, I would recommend that you check out filters and listeners. If you have some code that you want to run through prior to a particular servlet gets called or preforms a particular action, implementing a listener might just do the trick. Conversely, if you care about managing various things prior to a session getting created, perhaps a filter would be useful.

Sending form parameters from JSP to Struts action class

I am implementing a search functionality in a website I am building, which involves searching by the md5 hash of the name of the file submitted and searching by the notes associated with each submitted file. So, I should detect as to which button is pressed "Search by MD5" or "Search by Notes". This is the code I have:
JSP code for the form:
<form id="search" name="search" action = "search.do"
method="POST" enctype="multipart/form-data">
<table align = "center">
<tr>
<th colspan="4" bgcolor="#004276"><font color="white">
Search for Sample
</th>
</tr>
<tr>
<td><input name="md5" type="text" value="${form.md5}"/></td>
<td><input name="md5search" type="submit" value="Search by MD5"/>
</tr>
<tr>
<td><input name="notes" type="text" value="${form.notes}"/></td>
<td><input name="notessearch" type="submit" value="Search by Notes"/>
</tr>
</table>
</form>
search.do is mapped to SearchResultsAction.java. Code in Java action class (SearchResultsAction) which handles the request is:
if(request.getParameter("md5search").toString().equals("Search by MD5")){
searchSubmissionsList = submissionsDAO.searchSubmissionsByMD5(form.getMD5());
}
if(request.getParameter("notessearch").toString().equals("Search by Notes")){
searchSubmissionsList = submissionsDAO.searchSubmissionByNotes(form.getNotes());
}
But the problem I am facing here is that, request.getParameter("md5search") and request.getParameter("notessearch") return null for some reason. I have been working on this for a while and have not been able to figure it out. The weird thing is that it once worked for me sometime back when I was working on another project. Am I missing something here?
It's null because you used multipart/form-data form encoding instead of (default) application/x-www-form-urlencoded. Basically, you have to (let Struts) extract the text fields from the multipart form data body the same way as you (or Struts) extracted the uploaded file. Or, as there is actually no <input type="file"> field in your form at all, just remove the enctype attribute altogether.
See also
Does form with enctype="multipart/form-data" cause problems accessing a hidden field

How to process the value from a a:href

I have a JSP page:
<table width="40%" cellpadding="5" bordercolor="#000066"
bgcolor="#FFFFFF" border="1" cellspacing="0">
<c:forEach var="contactInfo" items="${contactsList}">
<tr>
<td><div align="center">
<b><a href="requestDelete.jsp? id=${contactInfo.userID}">
<c:out value="${contactInfo.contactName}"/></a></b>
</div></td>
</tr>
While clicking the link, i need to obtain the user_id, so that i can load details from the mysql db by passing the user_id and the details will be displayed in the requestDelete.jsp.
How to get the user_id in my servlet class after clicking the link.. or i need to redirect the action to my servlet class...from there i can call the response page requestDelete.jsp
Please help..
<c:outvalue="${conatctInfo.contactName}"/>
This will create link like for example
requestDelete.jsp?id=50
Now you want this to be passed to some servlet so lets create one servlet, currently this will create a GET to jsp.
Create a servlet
Map it to some url pattern lets say /deleteUser
and modify your hyperlink to produce
/deleteUser?id=50
and in servlet's doGet()
long id= request.getParameter("id");
// some validation ans checks..
// call service to delete ..

Categories