jsp update specific record on click - java

Hello i have a JSP page that displays records from animal table in my database which contain id, name, date-of-birth and some other information
what i want when the user click on animal name will be redirected to updateServlet which will display another JSP page for updating
I was trying the following:
<td><a href="/Relay?update="+${view.animalId}>${view.animalName}</a></td>
where ${view.animalId} represents the animal_id column, and ${view.animalName} the animal_name and these records come from arraylist
is it possible to pass the animal_id in the link in jsp page ?

Yes, it is possible, but you were doing it wrong. You seem to think that HTML is a dynamic language and somehow part of the Java/JSP language. But this is not true. You should see JSP more as a HTML code generator. The following example should work for you:
<td>${view.animalName}</td>
This way it's as following available in the servlet mapped on /Relay:
String animalId = request.getParameter("update");
// ...

Related

Navigate results in content space

I hope you get my problem.
out.println("<li class='has-sub'><a href='#'>" + k1.getKName() + "</a>\n");
I have a JSP and inside this java code. The result is a navigation on the left side with several categories and subcategories. So this is one category element. As you can see, I didn't put anything in the href. What I want to do is, that when I click on this category, I will get the articles of this category in the content space on the right side.
So, what do I have to do with servlets or JSPs in order to give a result to the content space. I can't just call a servlet there of course, because that means that I get the result of the servlet inside the href obviously.
I am sorry if this is a silly question, but I really don't know how to solve this :(
Further to previous comments you do not need web services. You can do this using ajax and a normal Servlet. You might want to look at using JQuery to help with the Ajax part. Here's some JQuery documentation around the load() function which will:
Load data from the server and place the returned HTML into the matched
element.
https://api.jquery.com/load/
Your link will look something like (if k1 is a bean in some scope then you can use EL rather than scriptlets):
<a href='javascript:loadData(${k1.id});'>${k1.name}</a>
Your Javascript will look something like:
function loadData(id){
var url = "/pathToMyServlet?id=" + id;
$( "#result" ).load( url );
}
which will call your Servlet and insert the HTML returned to an element on your page with the ID 'result'.
Your Servlet then needs to generate the data and forward to a simple JSP which returns the results (and only the results) i.e. it does not need to be a fully formed HTML page but should only contain the table of results or whatever.
And stop using scriptlets:
How to avoid Java code in JSP files?

servlets / .jsp / sessons...confused

I am brand new to all of this, I like servlets and java on a whole so far but I'm having some growing pains.
Servlet Question: Just starting to learn servlets and I'm having an issue with Attributes/Parameters, Sessions and jsp.
Basically, I have a very basic
form. My servlet code states:
println("Hello, " + request.getParameter("name") + "!!!");
..and my .jsp states:
<form action="SimpleServlet" method="POST">
<input type="text" name="name">
<input type="submit"/>
</form>
So now what I need to do is take the last name input into this servlet, save it in session and then send it to a different .jsp that states:
'Hello, '
For example, if I input JIM into the servlet and it returns 'Hello, Jim!!!' in my first .jsp I would then need to click on a link on that page that re-directs me to another .jsp that takes the 'Jim' input and also displays 'Hello, Jim!'.
So I created a 2nd .jsp and have tried many different combinations of code on both to try and get this to work and I keep on getting null or screwing up the output of the first part of the form.
Could someone guide me in the right direction, I would greatly appreciate it.
if you want to show your last name JIM in multiple jsp pages you can use session to store your last name add following code in your servlet.
String name = request.getParameter("name");
HttpSession session=request.getSession();
session.setAttribute("name",name);
then in all your jsp you need to do is put following scriplet where you want to print your name.
<%
String name = request.getSession().getAttribute("name");
out.print(name);
%>
You can do exactly what you want using a single .jsp page, you don't have to create a different one. Just create a link that will POST different parameters to the same .jsp
But as an advice/direction to you, I think the first thing you must understand well is how can you pass and receive parameters and attributes to deal with at server side. Understanding what a request can carry has a huge importance in server-side development.
Just an addon to Mitul's answer
I'd advice you to use EL. It is a simplified approach.
Instead of String name = request.getSession().getAttribute("name"); use
${sessionScope.name} It would give you the desired output.
The best part is that scope resolution is done automatically. So, here name could come from page, or request, or session, or application scopes in that order. If for a particular instance you need to override this because of a name collision you can explicitly specify the scope as

JSP page - In a table <TD><input type = text name = "code"></TD>

I have created a JSP page with a table containing two columns Code and Description ,the table data is input type="text" name ="code", the functionality which i have to implement is,if i type the code number the description of the code has to generated automatically from the database could any one please help me
Why not you create a javafunction that contain a ajax call to your servlet or java file where you can write a code to get description based on code value and return back to jQuery function.
You can trigger jQuery function on "OnChange" event of code input box.
You can get various example and docs over google about ajax request.

Passing JSP data to Java class and vice versa on button click

I am fairly new in JSP and I am trying to figure out how to pass data entered in the form on a JSP page to the java class and send this data back to the JSP page on click.
My index.jsp looks a bit like this:
<%# page import="mypackage.*" %>
<% myClass c = new myClass();
c.setString("String"); %>
<p>This is a test: <%= c.getString(); %></p>
The above code will output "String". I can access my class with no problems if I set the value on page load. I tried using servlets after some research. I modified my form to add the servlet "testServlet" on form action:
<form method="POST" action="testServlet">
Then on the doPost() method in testServlet I added in this:
String myString = request.getParameter("myString"); //myString is also the name of my textbox
System.out.println("Entered string: " + myString);
However, I am clearly missing a crucial part of the flow of how this should work and I am most probably wrong with this one as well as all the form does after I press submit is redirect to testServlet so I get the error that the resource is not available since it isn't a JSP/html page.
So my questions are, how exactly can I pass data from JSP to java and vice versa? Also, is there a possible way to do this without servlets? And what are good tutorials/examples for studying JSP and its behavior such as passing data? Please help.
After some more research, I am now able to transfer data set on index to detail using jsp:useBean however, as I've read in all the forums I've visited, servlets should be used to handle this but this really confused me more as a beginner so I really want to figure out what I'm missing here.
I've made sure that the servlet is registered on web.xml and I didn't change anything in there.
Update: I've tried re-creating the project from scratch and somehow managed to make the servlet work. As it turns out, I was missing something on the doGet() method. But now my question stands, is there a way to process the form without using servlets or page import in the JSP file? I was shown a sample code that didn't use servlets or page imports. I did take notice at tag.
use the java beans with getters and setters for there class member variables.

display tags jsp tags

i am using diaply tags library for displaying my tables.But i cant use the <% %>
tagst there.If i try to us it it gives me error.but i can use tag there.
If i try to use followin code in my jsp it give an error sayin shoul hav a matcheing ending tag.
i have follown java code in jsp
List<Course> = (List<Course>)request.getAattribute("crc");
here Course is a class/bean.
can anyone suggest me such library that i can use with struts for auto paging,displaying list in tables,and with other features provided by display tag.I want to use struts and i want the view to look good and yet easy to devlop.that is i want to achieve high class userinterface with littel effortr toward displaying o/p / view.
can anyone provide the example of disploay tag with struts
You can use the name attribute of the table tag (normally like this:)
<display:table name="crc" ...>
</display:table>
To use the crc List as the basis of Javabeans to display.
See http://displaytag.sourceforge.net/1.2/displaytag/tagreference.html#display-el:table for more information about the table tag.

Categories

Resources