[Dynamic Web Project] JSP - Servlet - web.xml [message #227708] |
Thu, 12 February 2009 09:33 |
lmhelp 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 #227806 is a reply to message #227769] |
Fri, 13 February 2009 14:30 |
lmhelp 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
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04236 seconds