Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » ServerTools (WTP) » [Dynamic Web Project] JSP - Servlet - web.xml
[Dynamic Web Project] JSP - Servlet - web.xml [message #227708] Thu, 12 February 2009 09:33 Go to next message
lmhelp is currently offline lmhelpFriend
Messages: 10
Registered: July 2009
Junior Member
Hi everyone,

Thank you for reading my post.
Let me explain you what I am trying to do and what my problems are.

I created a new "Dynamic Web Project" using Eclipse.
The "Project Explorer" view shows the following architecture:

(L1 = Level 1, ..., L5 = Level 5)
------------------------------------------------------------ --
1. L1 -- WebProject

2. L2 -- -- Java Resources
3. L3 -- -- -- src
4. L3 -- -- -- servlets
5. L4 -- -- -- -- (default package)
6. L5 -- -- -- -- -- ClassUploadFile.java
7. L3 -- -- -- Libraries
8. L4 -- -- -- -- Apache Tomcat v6.0 [Apache Tomcat v6.0]
9. L4 -- -- -- -- EAR Libraries
10. L4 -- -- -- -- JRE System Library [jre6]
11. L4 -- -- -- -- Web App Libraries

12. L2 -- -- JavaScript Support
13. L2 -- -- build
14. L2 -- -- WebContent
15. L3 -- -- -- jsp
16. L4 -- -- -- -- FormUploadFile.jsp
17. L3 -- -- -- META-INF
18. L3 -- -- -- WEB-INF
19. L4 -- -- -- -- lib
20. L4 -- -- -- -- web.xml

21. L1 -- Servers
22. L2 -- -- Tomcat v6.0 Server at localhost-config
------------------------------------------------------------ --

As you can see, I have created:
- a folder: "jsp" (line 15.),
- a "Source Folder": "servlets" (line 4.)
- two files: a JSP "FormUploadFile.jsp" (line 16.)
and a servlet "ClassUploadFile.java" (line 6.).

Roughly,
- the JSP is a HTML form that I want to use
to choose the file I want to upload,
- the servlet does the uploading job.

My first problem is the following:
I do not really understand the architecture above
and didn't find the reference documentation that
explains it.
I only had a glance at the file:
".settings\org.eclipse.wst.common.component"
which seems to be linked to the problem...
------------------------------------------------------------ --------------------
<?xml version="1.0" encoding="UTF-8"?>
<project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="WebProject">
<wb-resource deploy-path="/" source-path="/WebContent"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src"/>
<wb-resource deploy-path="/WEB-INF/classes"
source-path="/servlets"/>
<property name="context-root" value="WebProject"/>
<property name="java-output-path"/>
</wb-module>
</project-modules>
------------------------------------------------------------ --------------------

Tiebreaker: what is that "default package" that was created?
What to do with it?

Second problem linked to the first one:
using the Eclipse internal web browser, I type in the URL:
http://localhost:8080/EtiquettesWebProject/jsp/FormUploadFil e.jsp
and I properly get my form.
I think it would have been more appropriate to type in the URL:
http://localhost:8080/EtiquettesWebProject/WebContent/jsp/Fo rmUploadFile.jsp
What is the rationale behind it?

Third problem:
I have to customize the "web.xml file".
Here is what is presently looks like:

------------------------------------------------------------ --------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"
version="2.5">

<display-name>WebProject</display-name>

<servlet>
<display-name>ClassUploadFile</display-name>
<servlet-name>ClassUploadFile</servlet-name>
<servlet-class>ClassUploadFile</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ClassUploadFile</servlet-name>
<url-pattern>/FormUploadFile</url-pattern>
</servlet-mapping>
</web-app>
------------------------------------------------------------ --------------------

Can you tell me if something is wrong whith this
because when I submit the form, the servlet is not
executed: the form is re-printed on the screen
and re-initialized.

Here is the "FORM" in the JSP file:
------------------------------------------------------------ --------------------
<FORM ENCTYPE="multipart/form-data" ACTION="" METHOD="POST">
<TABLE STYLE="background-color: lightgreen;" CELLPADDING="5">
<TR>
<TD> Choose a file to upload:
</TD>
</TR>
<TR>
<TD><INPUT NAME="uploadedfile" TYPE="file" style="width:
227px"/></TD>
</TR>
<TR>
<TD COLSPAN="2"><INPUT TYPE="submit" VALUE="Submit" /></TD>
</TR>
</TABLE>
</FORM>
------------------------------------------------------------ --------------------

Thanks in advance for your help,
--
Lmhelp
Re: [Dynamic Web Project] JSP - Servlet - web.xml [message #227718 is a reply to message #227708] Thu, 12 February 2009 09:35 Go to previous messageGo to next message
lmhelp is currently offline lmhelpFriend
Messages: 10
Registered: July 2009
Junior Member
From Konstantin Komissarchik:

Questions about the use of WTP are best posted on the newsgroup. This
mailing list is for discussing development of WTP itself.

http://www.eclipse.org/newsportal/thread.php?group=eclipse.w ebtools

> > Tiebreaker: what is that "default package" that was created?
> > What to do with it?

When you don't create any folders to hold your classes (called packages in
Java), your classes are considered to be in a "default package". Another
way
of thinking about it is that these classes do not have a package. Note that
the package namespace is relative to the directory you specify as java
source root. In your case, it looks like you designated the servlets
directory as a source root. The default package node that you are seeing in
the Project Explorer doesn't correspond to anything physical on disk. It
just there to help you understand the above situation. If you are happy
with
your classes not having a package specified, then you don't need to do
anything.

> > Second problem linked to the first one:
> > using the Eclipse internal web browser, I type in the URL:
> > http://localhost:8080/EtiquettesWebProject/jsp/FormUploadFil e.jsp
> > and I properly get my form.
> > I think it would have been more appropriate to type in the URL:
> >
http://localhost:8080/EtiquettesWebProject/WebContent/jsp/Fo rmUploadFile.jsp
> > What is the rationale behind it?

The WebContent directory is the root for the web content contained in your
app. The reason that the web content root is not set as the project root is
that you don't want to be picking up various project metadata files, java
source files, etc when resolving URLs. You can control the directory that's
designated as the web content root either at project creation or in project
properties (search for web).

> > Can you tell me if something is wrong whith this
> > because when I submit the form, the servlet is not
> > executed: the form is re-printed on the screen
> > and re-initialized.

You aren't specifying the servlet as the target of your action in your HTML
form element. The standard HTML behavior in this case is to send your POST
request to URL that originates it, which is what you are seeing.

- Konstantin
Re: [Dynamic Web Project] JSP - Servlet - web.xml [message #227732 is a reply to message #227718] Thu, 12 February 2009 10:13 Go to previous messageGo to next message
lmhelp is currently offline lmhelpFriend
Messages: 10
Registered: July 2009
Junior Member
Hi Konstantin,

Thank you for your answers.
For the two first points, I am ok.

For the last one:

> You aren't specifying the servlet as the target of your
> action in your HTML form element.

That's true, this is an example I imitated.
At first, I also thought it was strange to have an empty "ACTION" attribute
for the "FORM" element of the JSP but I also thought the "web.xml" file
did
and was meant to do the necessary "mapping" between the JSP and the
servlet...

Can you please tell me what you would put in the "ACTION" attribute of the
"FORM" element ; or how to solve the problem using the "web.xml" file?

Thanks for your help,
regards,
--
Lmhelp
Re: [Dynamic Web Project] JSP - Servlet - web.xml [message #227745 is a reply to message #227732] Thu, 12 February 2009 12:03 Go to previous messageGo to next message
lmhelp is currently offline lmhelpFriend
Messages: 10
Registered: July 2009
Junior Member
-- Really do not manage to find the right value for the "ACTION"
attribute of the "FORM" element... please tell me according to
the project architecture I "drew" in my first post.

-- Wondering if the "web.xml" file has to be "configured",
after the "ACTION" attribute has been set properly...

Regards,
--
Lmhelp
Re: [Dynamic Web Project] JSP - Servlet - web.xml [message #227769 is a reply to message #227732] Thu, 12 February 2009 17:21 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin KomissarchikFriend
Messages: 1077
Registered: July 2009
Senior Member
> That's true, this is an example I imitated.
> At first, I also thought it was strange to have an empty "ACTION" attribute
> for the "FORM" element of the JSP but I also thought the "web.xml" file
> did and was meant to do the necessary "mapping" between the JSP and the
> servlet...

The empty ACTION is used in cases where you have a single JSP or a servlet
that is capable of handling different app states depending on POST payload
or URL segments in a GET. In your case, you are trying to hand over from
the JSP to a Servlet, so you need to specify that. The value of the ACTION
attribute is simply a relative URL for where to send the form content. In
your web.xml file, you mapped your servlet to the FormUploadFile URL so
that's what you need to put in your ACTION attribute (or it might have to
be "../FormUploadFile" since your jsp file is nested in a jsp folder).
Play around with this and it might be helpful for you to find a book on
basic HTML and java web app development.

- Konstantin
Re: [Dynamic Web Project] JSP - Servlet - web.xml [message #227806 is a reply to message #227769] Fri, 13 February 2009 14:30 Go to previous messageGo to next message
lmhelp is currently offline lmhelpFriend
Messages: 10
Registered: July 2009
Junior Member
Hi, thank you for your answers.

> Play around with this

I did. Not such a nice play... :)

> and it might be helpful for you to find a book
> on basic HTML and java web app development.

Any more precise advice?
I would really prefer a resource on the Internet
if you know one, because there are no bookshops
near the place where I am presently.

OK. Here I have a more accurate example to submit to you.
What is it supposed to do:

- first thing:
the user fills in a form with his name and first name.
[Cf. FormNameFirstName.jsp]
For example: name = FOO, first name = BAR.

- second thing: those informations are transmitted to a servlet
which adds a suffix to the name and first name.
[Cf. ServletFormNameFirstName.java]
For example: suffix for name = popol, suffix for first name = momol.
(You get: FOOpopol and BARmomol).

- third thing: the servlet sends the "augmented" name and first name
to another (response) page which displays:
Your name is ...
[Cf. ResponseNameFirstName.jsp]
For example: You name is BARmomol FOOpopol.

------------------------------------------------------------ -------------
Here is the architecture of the project:
------------------------------------------------------------ -------------

1. -- SmallExampleWebProject
2. -- -- Deployment Descriptor: SmallExampleWebProject
3. -- -- Java Resources
4. -- -- -- servlets
5. -- -- -- -- ServletFormNameFirstName.java
6. -- -- -- Libraries
7. -- -- -- -- Apache Tomcat v6.0
[...]
8. -- -- build
9. -- -- WebContent
10. -- -- -- META-INF
11. -- -- -- WEB-INF
12. -- -- -- -- lib
13. -- -- -- -- web.xml
14. -- -- -- FormNameFirstName.jsp
15. -- -- -- ResponseNameFirstName.jsp

------------------------------------------------------------ -------------
Here is "FormNameFirstName.jsp":
------------------------------------------------------------ -------------

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<TITLE>Form name, first name</TITLE>
</HEAD>

<BODY>
<DIV STYLE="background-color: teal; color: white; font-weight: bold;
padding: 5pt;">
Form name, first name
</DIV>
<FORM ACTION="servlets.ServletFormNameFirstName" METHOD="POST">
<TABLE>
<TR>
<TD> Name </TD>
<TD> <INPUT NAME="input_name" VALUE="FOO" TYPE="text" SIZE="20"
/> </TD>
</TR>
<TR>
<TD> First name </TD>
<TD> <INPUT NAME="input_first_name" VALUE="BAR" TYPE="text"
SIZE="20" /> </TD>
</TR>
<TR>
<TD></TD>
<TD> <INPUT TYPE="submit" VALUE="Submit"> </TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>

------------------------------------------------------------ -------------
Here is "ServletFormNameFirstName.java":
------------------------------------------------------------ -------------

package servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletFormNameFirstName extends HttpServlet
{
private static final long serialVersionUID = 1L;

public ServletFormNameFirstName()
{
super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException
{
String strName = request.getParameter("input_name");
strName = strName + "popol";

String strFirstName = request.getParameter("input_first_name");
strFirstName = strFirstName + "momol";

System.out.println("strName = " + strName);
System.out.println("strFirstName = " + strFirstName);

request.setAttribute("attrName", strName);
request.setAttribute("attrFirstName", strFirstName);


getServletContext().getRequestDispatcher("/ResponseNameFirstName.jsp ").forward(request,
response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException
{
doGet(request, response);
}
}

------------------------------------------------------------ -------------
Here is "ResponseNameFirstName.jsp":
------------------------------------------------------------ -------------

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<%
String strName = (String) request.getAttribute("attrName");
String strFirstName = (String) request.getAttribute("attrFirstName");
String s = new String("Your name is " + strFirstName + " " +
strName + ".");
%>


<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;
charset=ISO-8859-1">
<TITLE>Response form name, first name</TITLE>
</HEAD>

<BODY>
<DIV STYLE="background-color: teal; color: white; font-weight: bold;
padding: 5pt;">
Response form name, first name
</DIV>
<DIV>
<%= s %>
</DIV>
</BODY>
</HTML>

------------------------------------------------------------ -------------
And here is the "web.xml" file:
------------------------------------------------------------ -------------

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"
version="2.5">
<display-name>SmallExampleWebProject</display-name>

<servlet>
<servlet-name>ServletFormNameFirstName</servlet-name>
<servlet-class>servlets.ServletFormNameFirstName</servlet-class >
</servlet>
<servlet-mapping>
<servlet-name>ServletFormNameFirstName</servlet-name>
<url-pattern>/FormNameFirstName.jsp</url-pattern>
</servlet-mapping>
</web-app>

Ok. I think everything we need has been reported here.

So what I want to know is:

1. In "FormNameFirstName.jsp":
<FORM ACTION="" METHOD="POST">
what to put in the "ACTION" field?

2. In "web.xml":
- what to put in:
<servlet-class></servlet-class>
- what to put in:
<url-pattern></url-pattern>

The values above are not ok, the form data are not transmitted
to the servlet. I get: Your name is nullmomol nullpopol.

Please help.
If you possibly know a documentation that would help,
I'd appreciate.
Regards,
--
Lmhelp
Re: [Dynamic Web Project] JSP - Servlet - web.xml [message #227843 is a reply to message #227806] Fri, 13 February 2009 22:33 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin KomissarchikFriend
Messages: 1077
Registered: July 2009
Senior Member
>> and it might be helpful for you to find a book
>> on basic HTML and java web app development.

> Any more precise advice?
> I would really prefer a resource on the Internet
> if you know one, because there are no bookshops
> near the place where I am presently.

Two points of practical advice:

1. It helps to target your questions to the most appropriate forum. This
newsgroup is about Eclipse Web Tools. It is good for getting answers
regarding the uses of this tooling, but not so good for getting answers to
general HTML and Java web app development questions. Sun (as the source of
the Java technology) is a good place to look for information regarding
Java web app development. I suspect that you will find docs, articles and
forums on the subject if you look around on http://java.sun.com/.

2. Regardless of the forum, the most you can hope for is that people will
give you tips and help point you in the right direction. Don't expect to
be able to post your source code and have others fix the problems for you.
People generally expect to be paid for that level of support. ;)

- Konstantin
Re: [Dynamic Web Project] JSP - Servlet - web.xml [message #227870 is a reply to message #227843] Mon, 16 February 2009 08:58 Go to previous messageGo to next message
lmhelp is currently offline lmhelpFriend
Messages: 10
Registered: July 2009
Junior Member
> Don't expect to be able to post your source code and have others fix
> the problems for you.

Quite a lot of forums like to have to whole source code to figure out
what the problems are.

Moreover I perfectly know I may not have a specific answer posting on a
forum: as you say, if I wanted such a thing I would pay someone for that.

I only thought that details were lacking in my first post and I
wanted to complement it.

It is true that you are working for free when you help me but me too.
And quite a lot of other people may benefit freely of our exchanges.

It is true that you do not know me but when I find the answer to my
problems I always post the solution on the forums that helped me. So
we both work for free. I think other people helped you too in the past.
This is the principle of the "forum approach".

> the most you can hope for is that people will give you tips and help
> point you in the right direction.

I am not expecting anything more than that.

A very nice person sent me a private message to give me a website
reference. I haven't yet been able to examine it but I'll do it today
and be sure that if I find the answer I'll post it here.

--
Lmhelp
Re: [Dynamic Web Project] JSP - Servlet - web.xml [message #227898 is a reply to message #227870] Mon, 16 February 2009 11:07 Go to previous message
lmhelp is currently offline lmhelpFriend
Messages: 10
Registered: July 2009
Junior Member
Hi to everyone.

Thanks to some help I got, I made my little stuff work.
Here are the two modifications that have to be made in the code
I posted previously:

1. In "FormNameFirstName.jsp" replace the "FORM" element with:

------------------------------------------------------------ ------------------------------------------------------------ -----
<FORM ACTION="ServletFormNameFirstName"
METHOD="POST">
------------------------------------------------------------ ------------------------------------------------------------ -----

2. Replace the former "web.xml" with that one:

------------------------------------------------------------ ------------------------------------------------------------ -----
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>SmallExampleWebProject</display-name>
<servlet>
<servlet-name>ServletFormNameFirstName</servlet-name>
<servlet-class>servlets.ServletFormNameFirstName</servlet-class >
</servlet>
<servlet-mapping>
<servlet-name>ServletFormNameFirstName</servlet-name>
<url-pattern>/ServletFormNameFirstName</url-pattern>
</servlet-mapping>
</web-app>
------------------------------------------------------------ ------------------------------------------------------------ -----

Like this, it works.

--
Lmhelp
Previous Topic:wsgen ant task <-> preferences
Next Topic:deploy to apache
Goto Forum:
  


Current Time: Fri Apr 26 21:50:16 GMT 2024

Powered by FUDForum. Page generated in 0.04480 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top