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
Related
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
I am trying to add some java variables in html using jsp.
html
.... name="Editbox1" value="<%=a %>" spellcheck="false">
I have 7 boxes. From a to g.
At the end of the code, i have a button that should add these, but it wont.
input type="submit" id="Button1" onclick="<% sum = a+b+c+d+e+f+g; %> "
when this buton does its thing, the answer should be seen in the editbox number 8.
You're mixing things that cannot mix - JSP code runs on the server (it is basically a servlet), while html/js code runs on the client (browser).
When the JSP file is processed on the server, all <% ... %> tags are replaced by their evaluated results, and in the end only the html/js is returned to the client.
For this reason, if you take a look at the source code in your browser, you'll see the onclick handler is defined with an empty string - the JSP code evaluates to nothing.
If you want to summarize values on button click, you'll either have to do it in JavaScript, or send a new request to the server to get the result.
So I have a .jsp that displays the header part of my web application. This is a very standard looking page. I have a bar at the top with things like Login, Register, Contact Us, etc... And below that we have a horizontal navigation bar.
This is a standard Spring web application with a maven build. It produces a file, web.war, that we deploy to tomcat. on dev, this works fine.
We are testing production, and want this war to be the root, so we rename the war to ROOT.war and restart. everything fine.
Now sometimes, we get the navigation section from a Content Management System. We set the text as a variable and display that. No obviously we can't use ${pageContext.request.contextPath} as that wont get parsed this way, so before we display the text we do this in the jsp
<% navcopy = navcopy.replace("${pageContext.request.contextPath}", request.getContextPath()); %>
Now this is adding the /web to the URL. Now this is baffling to me as registration link in the jsp works:
<a id="home-left-menu-item" href="${pageContext.request.contextPath}/register.html">Register Now!</a>
So hoe does ${pageContext.request.contextPath} parsed in the jsp return the correct root but the call request.getContextPath() returns "/web"
Aren't they essentially the same call to the same object to the same method?
Any insight would be appreciated.
${pageContext.request.contextPath} : Returns the portion of the request URI that indicates the context of the request.
In fact, it is identical to request.getContextPath(), since ${pageContext.request} refers to the HttpServletRequest of the current request.
For example:
http://localhost:80/myProjectName/path/servlet
${pageContext.request.contextPath} returns /myProjectName
request.getServletPath() Returns the part of this request's URL that calls the servlet, e.g. /path/servlet
${pageContext.request.servletPath} returns /path/servlet
For this code try this:
<a id="home-left-menu-item" href="<%=request.getContextPath()%>/register.html">Register Now!</a>
I want to perform an action on a JSP if redirected from a specific servlet only else do nothing.Is it possible?
In my JSP there are different errors defined. This JSP calls a servlet (with contentType as application/pdf) which opens in a new tab and searches for a PDF for 25 seconds and then if PDF is not found redirects to same JSP which shows the error message "File not found". I want to show the error if called from servlet only else do nothing.
JSP Code:
<%}else if(hPP!=null && hPP.get("errorcode")!=null && hPP.get("errorcode").toString().equalsIgnoreCase("Issue")){%>
<c:if test="${cameFromServlet}">
<div class="SplInputField">
<label class="FontBlod">Download fail</label>
</div>
</c:if>
servlet code
if (content == null) {
request.setAttribute("cameFromServlet", true);
String redirectJspUrl = request.getParameter("homeRedirect");
String strReceiptPage =
redirectJspUrl.substring(0, redirectJspUrl.lastIndexOf("/")) +
"/GetQReceiptPage";
response.sendRedirect(strReceiptPage);
}
Add an attribute to the request in the servlet like this
httpservletRequest.setAttribute("cameFromServlet", true)
then in your JSP check for it
<c:if test="${cameFromServlet}">
DO STUFF HERE
</c:if>
EDIT:
What you have done in your edit will not work, since you are doing a redirect. Which means the browser is sent a 302 response to tell it to issue another request against the new url. Do you have a specific requirement to change the url for the user? If so you will need to add the cameFromServlet attribute to the session instead - like this:
req.getSession().setAttribute("cameFromServlet", true);
Bare in mind though, that cameFromServlet attribute will remain on the session until you unset it so if there was another time that jsp page is shown you will run into problems unless you do something to unset it - either by introducing another servlet in the middle and moving it from the session to the request - thus simulating Springs flash map behaviour or unset it in the JSP after you have used it - like this:
<c:remove var="cameFromServlet" scope="session" />
If you do not need the URL to change for the user, you can change your servlet code to make use of a request dispatcher (what I thought you were doing)
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/yourjsp.jsp");
requestDispatcher.forward(req, resp);
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 ?