jsp page dosent load css when using request dispatcher - java

enter image description hereso i was trying to load a jsp page when the submit button was clicked when i press the submit button data isent to the servlet.so the page loads but without the css.
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/answerPage.jsp?id=" + id);
requestDispatcher.forward(request, response);
this didnt work so i tried this.
response.sendRedirect("answerpage.jsp");
it just gave me the 404 error

Without seeing the code to your answerPage.jsp, I would ensure that you have the css page linked in the html, something like:
<link rel="stylesheet" type="text/css" href="styles.css">
And just make sure that the css file is located in the WEB-INF folder in your project

so i found the answer to my problem.the problem was in the url mapping of my xml file

Related

Servlet loads a new page

I have the following problem:
I have two servlets, one returns a navigation. There are categories and subcategories that the servlet gets through the DAO. I use the following script to get the servlets result on the right position:
$(document).ready(function() {
$('#navigation').load('Navigation');
$('#content').load('Content');
});
Below my navigation div, I have a content div. As you can see, i am loading the content-servlet. In my navigation; i have the following elements:
<li><a><form action='Content' method='get'><button type='submit'>"
+ k2.getKName() + "</button><input type='hidden' name='category' value='"+ k2.getKategorieNr() + "'/></form></a></li>
Now when I click on a subcategory, the button will call the content servlet. The problem is, that it loads a new page without the navigation. So I can only see the code that the servlet returns in a new page.
How can I get the result of the content servlet inside my previous page as it already works for my navigation servlet?
When you load your page initially, $("#navigation").load("Navigation") makes an AJAX request to your Navigation servlet and pastes the HTML response from the servlet back into an element like <div id="navigation">.
When you click your button in the form, you are making a top-level full page request to the same servlet, and the response content will replace the whole browser window, not paste it into the page like you are getting with jQuery load.
The easiest way to address this may be with the jQuery ajaxForm plugin:
http://malsup.com/jquery/form/
where you would do something in your ready callback like
$(document).ready(function() {
$('#navigation').load('Navigation');
$('#content').load('Content');
$('#formId').ajaxForm({target: '#content'});
});
Of course you need to add an ID to your form element so you can easily find it with jQuery.
See
jQuery ajax - change jQuery target value with form submit

how to redirect in jsp using <jsp:forward> with a relative url

I'm trying to redirect from a page to another
always use
<jsp:forward page='/some_page.jsp' />or
response.sendRedirect(/some_page.jsp)
but in this case I need to redirect to another page
someting like home/try/page and I try to use
<jsp:forward page='home/try/page' /> but it doesn't work some help?
<jsp:forward /> tag is intended for forwarding the request on to a relative url.
now you want to redirect your page here - 'home/try/page'. If your want to redirect from one directory to another directory page than try something like below -
<jsp:forward page="/home/try/page.jsp"/>
<jsp:forward page="../home/try/page.jsp"/>
or
<jsp:forward page="../../home/try/page.jsp"/>
"../" will get you out from the current folder.
For example - let assume we requested the following URL, -
http://localhost/myJSPApp/Security/login.jsp
When the file "login.jsp" executes it performs the following action
<jsp:forward page="../Welcome/Welcome.jsp" />
This is a forward to another JSP in another folder "Welcome" which is at the same level as the "Security" folder.
I hope it will help you.
It looks like you may need to move up the "tree" to get to a different folder. For example, if you are current in page x.jsp in folder "foo" and need to get to page y.jsp in folder "bar", you would need to do this
- jsp: forward page='../bar/y.jsp'.
The "../" tells jsp to back out of the current folder(foo) it is in and look for the next folder(bar). You would need to add one "../" for each level you go up.
Hope this helps.

How to display 2 html pages in one html page with javax.swing.text.html.HTMLEditorKit

I got in a database some html textparts. Normally they should make one whole html mail body with one html tag and one html close tag and also one for the body.
Sometimes these textparts contain for each line a html open tag and close tag and also a body open and close tag like this:
<html>
<body>
test1
</body>
</html>
<html>
<body>
test2
</body>
</html>
If I try to display a html page with 2 full webpages like in the example above in google chrome or IE of firefox it displays the content correctly and just displays the second page underneath the first. Also inside a mail client that uses html layout this works out fine.
Now in an application I have to display the content of the mail body in a htmlviewer.
I use javax.swing.text.html.HTMLEditorKit for this and this works fine if I have only one page. But when I got 2 or more it just shows the first one. I tried to embed the code inside another html tag but this didn't solve the issue.
Is there a method to display this webpage without rewriting the html code?
Maybe some option of javax.swing.text.html.HTMLEditorKit ?

Displaying Loading gif or waiting message until servlet loads

I have a application that invokes a servlet through the URL
> "http://server:port/context-root/myservlet"
The servlet then calls the Java Class which returns the query result back to servlet.The servlet then renders the data to the user through a JSP page(response.redirect)
Now it hapens so, when all this happens Page Cannot be displayed is rendered to the useruntil the JSP page is ready to show the data.
How can I show a loading gif or a messgae as soon as the servlet is invoked until the JSP page is loaded with all the required data:
NOTE: As mentioned above, I am first calling the servlet, then Java Class, then JSP.
#Sankalp - The invoking application (HTML page ) is solely responsible for making an AJAX Call to your servlet. If you dont have control over the invoking application here is a little trick you can do - Ask the invoking application to re-direct to an html file say an index.html file of your application . In the index.html file , export jQuery javascript library and make appropriate ajax call , show a loading image , and upon success you may re-direct the page to desired jsp. There is a lot of work to be done here.
Question : Does the invoking application pass you any parameters ?
Does it POST data to you ?
Does your application open in an IFRAME of the invoking application or its a pure re-direct ?
All these answers , would help you decide your next course of action. There are lots of post on AJAX calls and showing images on stack overflow but that does not solve your essential problem . You have to decide on the flow and where to put the AJAX code. The AJAX part would be the easiest one. :)
----- Editing after the last comment
Visit : jQuery
In your HTML
<html>
<head>
<script src="jquery.min.js"></script> <!-- where you keep your resource file -->
<script language="javascript" type="text/javascript">
$(document).ready(function() { //This call will be made when DOM
//hierarchy has been fully constructed
// Handler for .ready() called.
//Make AJAX Call here so that this simple HTML page
///directly calls the AJAX
// and decide the future action based on AJAX success / failure
});
</script>
</head>
<body>
</body>
</html>

URL in JSP always goes to localhost and gives HTTP Status 404

I'm using a servlet to generate an html page, and in the java I code the URL to output a standard href and it seems to output fine on the page, however when I click on it, I get HTTP Status 404.
In my servlet the code is :-
out.write("<html>");
out.write("<body>");
out.write("<br/>");
out.write("<a href=\"url\"" + "facebook.com" + "\">" + "facebook.com"
+ "</a>" + "<br/>");
out.write("</body");
out.write("</html>");
And it looks fine in the generated html page when I view source.
<html>
<body>
<br/>
facebook.com
<br/>
</body>
</html>
However everytime I click it the link appears as .http://localhost:8080/MyProject/url and of course this goes to HTTP Status 404 - /MyProject/url.
Anything I can do to get it to actually go to a URL, i.e. facebook.com
I'm using Tomcat 7 as my app server.
Thanks
Make it
out.write("<a href='facebook.com'>facebook.com</a> <br/>");
Generating view from servlet is bad idea, use JSTL instead

Categories