How show label message in JSP - java

Could you please help me in displaying message after certain work done like 1 record deleted after
deleting data from database.
In JFrame there is a method like setVisible(true), similarly is there any way in JSP?

JSP is just a HTML code generator. All you need to do is to make sure that it generates HTML the way you want. You can use JSTL core tags to control the flow of HTML code generation.
For example, assuming that you've a servlet which sets a message like follows,
request.setAttribute("message", "Record successfully deleted");
then you can conditionally display it in JSP as follows with help of JSTL <c:if>.
<c:if test="${not empty message}">
<span class="message">${message}</span>
</c:if>
See also:
Our Servlets wiki page - contains Hello World example with similar validation/messaging approach
Our JSTL wiki page - contains info how to install and use JSTL

Related

Extracting and processing textarea value form without changing the page

I have a JSP file in which there are two textareas and a submit button all in a form ;
<form action="" method="post">
<textarea name="inConsole" id="in" cols="100" rows="10"> </textarea> <br/>
<input type="submit" name="Send Command"/> <br/>
<textarea name="outConsole" id="out" cols="100" rows="10"></textarea>
</form>
this page is supposed to work like any SQL program. so the user types a command in the first textarea and clicks submit, then the value of textarea will be extracted to a field and a method will take care of the command and return a log (1 row inserted, error:bad syntax etc) which will be displayed in the second textarea.
I know how for example make a login page and send data and redirect user to a new page(new jsp) file if user pass is correct.
what I can't find is how can I do all the things that I said above without going to a new page while using form action.
I have checked other questions that linked the action attribute to a servlet which was confusing for me( the way that a servlet was called). I'm looking forward to use a simple scriptlet for this purpose like the one I used for my login page:
<%
DatabaseLoginTest dbLogTest = new DatabaseLoginTest();
if (dbLogTest.DBLoginChecker(request.getParameter("user"), request.getParameter("pass")) == true){
%>
<p>Login Successful</p>
<% } else { %>
<p>Login Failed</p>
<% } %>
also I'm aware that adding java scripts(not Javascript scripts:) ) to html isn't a good practice(and the reasons for it) but I think this might be easier for a simple program that I'm working on.
p.s: I'm using tomcat and Intellij for developing this web application
and I have made a custom SQL so I only need the code that gives me the textarea value and the one that sets the other one's value
Update: now I know I should use javascript but I don't know how can I send the data extracted by javascript to a java method.
If you want to do this while remaining in the same page, you have to use Javascript. This is because if you want the server to be able to re-render the page, there has to be a page refresh.
You would need to write onClick handler for the submit button and make a Ajax call to your server to a specific URL with the user input. This URL would serve the data needed for the necessary UI changes.
You can use a scriptlet to generate the HTML that would be shown in the webpage but this would only suffice for a simple use-case and it would be a lot simpler if, say, your service returned just the data required to make the UI change and actual UI change is handled by the JS.
Also,I don't think it is a bad practice to embed JS in HTML. Sure, you can optimize this by including a JS source file but that's a separate optimization.

JSP page with dynamic html

I've been trying to write a website in which all navigation is handled by hiding and showing divs. It is my understanding that this method is called Single Page Interface. This has worked for simple designs in the past but my current task is starting to become very troublesome using this method. How would I go about replicating the same behavior but instead of hiding and showing divs I can just have a main container div that is then populated with the desired html from the server?
Example:
<script>
$("#button").onclick(function() {
$("#a").show();
$("#b").hide();
});
</script>
<html>
<body>
<div id="a" style="display:none;">A: SOME HTML</div>
<div id="b" style="display:block;">B: SOME HTML</div>
<button id="button">Change to A</button>
</body>
</html>
(note this is a very rough example of white I'm trying to do)
But I would like the contents of a container div to change from "B" to "A" via some jsp
Could anybody point in the correct direction?
Further Explanation:
Maybe I can clarify a little better. So when the user loads the page they are presented with a section that has a table of all the existing files in a database. The user can select a file from the DB list to rename or copy. If the user wishes to rename a file, for example, they would be presented with a new display (all within the same "Tab") which will have a set of fields populated for the file that they have selected and a set of empty fields in which they can specify the new file name. Currently this changing of displays is handled by showing and hiding divs, but I would like to retrieve the html that I want to display from the server and present it. Basically mimicking the hiding and showing of divs.
As it's not completely clear to me what you're trying to do I'll give you some options:
Replace the content of a element on your page see
Since you're using a JSP, you can use server side logic to display certain fragments
You're using a JSP, use that to render some server side content
Ad 1:
(assuming jQuery) $('body').load('serverSide.html'); see http://api.jquery.com/load/
Ad 2:
<% if ("a".equals(request.getParameter("aOrB"))) { %>
<jsp:include page="/a.jspf">
<% } else { %>
<jsp:include page="/b.jspf">
<% } %}
Ad 3:
<%= request.getAttribute('content') %>
Hope that helps

asynchronous widget JSP foreach (Java EE application)

I have the following code in a JSP page:
<c:forEach var="widget" items="${widgets}">
<p><h2>Widget</h2></p>
<p>IDType: ${widget.id}</p>
<p>Name: ${widget.name}</p>
</c:forEach>
At the moment it is only text that will be displayed, but in the future there will be charts and images as well.
I would like to use asynchronous loading, so the page doesn't have to wait on the largest images/charts.
On the internet I found tutorials for Java code, but not for JSP pages. What is the best way to implement asynchronous loading in JSP pages?
Thanks!
Simply generate these images and charts separately using different servlet:
<c:forEach var="widget" items="${widgets}">
<p><h2>Widget</h2></p>
<p>IDType: ${widget.id}</p>
<p>Name: ${widget.name}</p>
<img src="chart-servlet.png?id=${widget.id}"/>
<img src="image-servlet.png?id=${widget.id}"/>
</c:forEach>
On the server side map some servlets to chart-servlet.png and image-servlet.png. You can then use id parameter to generate appropriate chart or image. From the browser perspective it first renders DOM without images to then ask server for external resources (images).

java if statement to check if object is empty

hi I am new to java and I am creating a jsp and I am using scriplet code. I would like to be able to have my object to not display on the screen when it is empty.
<% if (webApp.getInfo != null) { %> <h6><%=webApp.getInfo()%> <% } %>
Currently it is checking if the object is null but it is still displaying a line on the page when I run the JSP. How can I check to see if webApp.getInfo is empty and not have a line display or test display for those headers?
<c:if test="${!empty str}">
<h6>...</h6>
</c:if>
Note that:
this is meaningful only for strings, not for any object
this is JSTL, which is preferred to scriptlets (from your example)
your objects needs to be set as request attribute.
you only need this if you have additional tags inside the if-clause. If it is only the string that you want to output, there is no need to check - as BalusC noted this is not displayed.
you can trim whitepsaces from the jsp output using configurations provided by your servlet container, or if using servlet 3.0 - via <trim-directive-whitespaces> in web.xml

Display form-processing results on current page in JSP

Currently I'm able to send some form data from an HTML page to a Servlet, process it, and return the result by forwarding/redirecting to a displayresults.jsp page; However I'm wondering if there's a way to process a form submission in this fashion and then return the results to the initial page? So that whatever I wish to return shows up below the form.
The idea in the end will be to have a search function up top, then have the search results appear below on the same page.
<c:choose>
<c:when test="${empty param.search}">
<form method="post"><input type="text" name="search" /></form>
</c:when>
<c:otherwise>
<%-- show search result here. --%>
</c:otherwise>
</c:choose>
Yes
Stick the results into the request object from within the servlet
Get the servlet to forward back to the request page
Add some code into the JSP to pull out the results from the request object and display them
See this for a simple example http://www.tek-tips.com/viewthread.cfm?qid=1189624&page=11
If your usecase means that you'll be jumping from the search results elsewhere and back you may wish to maintain the search results in the session.
Personally, I almost always put my "collect search criteria" and "display results" in the same servlet or JSP file. I write them with this basic structure:
if request data is present
collect search criteria from request object
check for errors
else // i.e. first time in
fill search criteria with blanks or defaults
end if
display error messages // if any, of course
display search criteria // either what we got from last cycle or defaults
if request data was present
process request
display results
end if
I like this structure because on an error, I'm set up to display the bad data, let the user fix just what was wrong, and then cycle back through. On success, I'm set to let him adjust the criteria and re-run. Everything is in one place but it's structured so it's not a mess.
When it's up to me I rarely use servlets: I prefer to use JSP for the top level and put the non-trivial, non-display code in other classes that I simply call, but that's an implementation detail. The principle works as well with servlets as with JSP.

Categories