What is the difference between the /urlpage and urlpage? - java

I can't understand why sometimes a '/urlpage' is used over a simple 'urlpage'.
for example on a form,
<form action="TestServlet">
<form action="/TestServlet">
I want to understand what is the proper way of specifying a path to a html, servlet, jsp, jsf and etc.

The first one is relative to the current path. For example, if you are on /appcontext/abc/def/Something, TestServlet will be on /appcontext/abc/def/TestServlet
In the second one, it is relative to the context of your app: if you are on /appcontext/abc/def/Something, /TestServlet will be on /appcontext/TestServlet

Related

How to get current jsp file path in war?

Due to lot of forward request, it is hard to identify the current showing jsp file. Is it possible to get the current jsp file path in war? How to achieve?
For an example:
URL: http://localhost:8080/controller/action
File Path: /view/dev/test.jsp
<%= getServletContext().getRealPath("/") %>
The above statement doesn't work, it show C:\Projects\test\build\web\ instead of C:\Projects\test\build\web\view\dev\test.jsp
This will be implementation dependent, but since many implementation use Jasper, it may well be more portable than you think. Of course, portability may not be an issue.
But the simple case is that you take the full qualified class name of "this" from within the JSP, and from that you can derive the path name. Each JSP is compiled in to a Servlet, and they each need their own unique class.
For example, in Glassfish, which uses Jasper from Tomcat/Apache:
I have a simple jsp, root.jsp:
<html>
<body>
<% out.println(this.getClass()); %>
</body>
</html>
It is in the root level of the WAR. When I run it, I get this:
class org.apache.jsp.root_jsp
When I created the directory d, and copied the root.jsp in to it, and executed it, I got:
class org.apache.jsp.d.root_jsp
So, you can see, that the class name for /root.jsp is org.apache.jsp.root_jsp. And for /d/root.jsp, it's org.apache.jsp.d.root_jsp.
If you remove org.apache.jsp from the class name, convert any remaining "." to "/", and the "_" to a "." you will get:
/root.jsp
/d/root.jsp
Which effectively gets you what you want.
If you overwrite a JSP at runtime, then it'll start sticking _1 and _2 to the end, so you'll have to account for that.
But play around with this, and it should work for you, and get you where you're going.
Just keep in mind, it's not portable, it clearly works in anything with Jasper (Tomcat, TomEE, Jetty, Glassfish), but I can't say about WebLogic or WebSphere.
But, it might. The container has to map the JSP file to a class somehow, and this is an intuitive way of doing it.
So, try it and see if you can get it to work with your container.

Difference between different form action attribute

Can any one tell in following cases where the request will be submitted at web application running at tomcat server:
<form action="register.abc">
<form action="/register.abc">
<form action="/Lab3/register.abc"> //Labx is the webproject name
when you start with "/" means that start from your root path. without "/" the url is based on you current local on your web site. For example.
You are here localhost:8080/Labx/mypage.html
<form action="register.abc"> // == localhost:8080/Labx/register.abc
<form action="/register.abc"> // == localhost:8080/register.abc
<form action="/Lab3/register.abc"> //== localhost:8080/Lab3/register.abc
In the first case 'register.abc' will be searched in the same directory where the page that contains it is located.
In the second and third cases the absolute path is provided, so it will be searched in the context of your website regardless where the calling page is, e.g.
http://host:port/Lab3/register.abc

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.

c:url tag includes jsession id query string

What is the best way of obtaining context-root on a jsp page. If I hardcode my css/image to "/myapp/images/foo.gif" then it will be a broken link if the context root / app name is changed. Using relative path is not always preferrable because a context root can be multi-path (eg: /mysite/myapp)
So far I've tried using <c:url var="root" value="/"/> which works alright (${root} will give the context-root /myapp/), however if this is the very first time user is visiting the site (or if cookie is cleaned on the browser), the value assigned to ${root} became something like /myapp/;jsessionid=019762g1hk3781kh98uihilho and it breaks the images/css reference. Is there any other better way than this?
So far I've tried using <c:url var="root" value="/"/> which works alright (${root} will give the context-root /myapp/)
This is not the right way. The <c:url> should be applied on every single URL individually.
You'd better use
<c:set var="root" value="${pageContext.request.contextPath}" />
See also:
Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP

What does this expression language ${pageContext.request.contextPath} exactly do in JSP EL?

I have a web app, where I have different navigation anchor tags such as Home, Profile and etc.
What I want:
When I press anchor tags like home or profile. I just want to ensure that current user gets its information in that Tags/JSP Page.
Sample Example that I am trying:
Profile
The pageContext is an implicit object available in JSPs. The EL documentation says
The context for the JSP page. Provides access to various objects including:
servletContext: ...
session: ...
request: ...
response: ...
Thus this expression will get the current HttpServletRequest object and get the context path for the current request and append /JSPAddress.jsp to it to create a link (that will work even if the context-path this resource is accessed at changes).
The primary purpose of this expression would be to keep your links 'relative' to the application context and insulate them from changes to the application path.
For example, if your JSP (named thisJSP.jsp) is accessed at http://myhost.com/myWebApp/thisJSP.jsp, thecontext path will be myWebApp. Thus, the link href generated will be /myWebApp/JSPAddress.jsp.
If someday, you decide to deploy the JSP on another server with the context-path of corpWebApp, the href generated for the link will automatically change to /corpWebApp/JSPAddress.jsp without any work on your part.
Include <%# page isELIgnored="false"%> on top of your jsp page.
use request.getContextPath() instead of ${pageContext.request.contextPath} in JSP expression language.
<%
String contextPath = request.getContextPath();
%>
out.println(contextPath);
output: willPrintMyProjectcontextPath
For my project's setup, "${pageContext.request.contextPath}"= refers to "src/main/webapp". Another way to tell is by right clicking on your project in Eclipse and then going to Properties:

Categories

Resources