I am new to forum. Could i get your help on my first Servlet?
I have created a basic servlet in Eclipse Juno , and I have a corrected servlet name and servlet mapping in my web.xml file but i did not understand why the system keeps complaining that it could not find my test servlet.It gavce me a status Http 404 error code.
I suspect that I may miss important points in my code, please help me if you could.
Also I'd like to include code for my html file which I use it to call my test servlet.
The problem is with your path. /mytest in your web.xml means http://host:port/webapp/mytest. /mytest in your HTML page means http://host:port/mytest. As you see the web application name is missing there. That's the entire issue.
The solution is simple - add the web application to the form's action. You can get that using request.getContextPath().
Thank you so much Gentleman for all of your help.
I was able to pass the error by the context path followed by the servlet path in my form action.
However , I was encounter another error saying that javax.servlet.ServletException: Error instantiating servlet class dung.com.MyTest.
I tried to look for a resolution in google , and someone suggested that
1. Clean Tomcat work directory.
2. Clean Tomcat.
3. Clean project.
4. Refresh project.
I am sorry Bear, I could not find servlet class file in my Eclipse Juno which I just downloaded this morning.
I was looking at my test application file structure, and all i found were the java source code of my test servlet but I couold not find its servlet class.
Attached is the image of the file structure that i captured in jpg.
I am still trying to find out why it did not generated the class files for my two java classes.
By the way, once you have all of the machinery working for you, you should change the code to use the POST method instead of GET. File uploads must be done via POST.
mike tran
Greenhorn
Joined: Jul 10, 2012
Posts: 8
posted
0
Thank you Bear, and Paul
I actual found out my test servlet class location. It was put under the \test\build\classes\dung\com
I actual changed my jsp page to POST method as you have indicated
and my servlet is as the same as before
However, it is now giving me a java NullPointerException at dung.com.MyTest.doGet(MyTest.java:33)
I look at my servlet code and it is complaining the
I looked at my jsp page and I found out that i already had the enctype="multipart/form-data" in my form action
First of all, when you post code could you put it in code tags in future? That makes it much easier to read. (That was me who added the code tags to your last two posts -- you can do it with the "Code" button.)
Anyway, it looks like you must have googled and come up with an old example of the Apache FileUpload code. My file upload code looks like this:
which corresponds to your NPE-producing code.
mike tran
Greenhorn
Joined: Jul 10, 2012
Posts: 8
posted
0
Thank you so much gentleman for all of your wonderful helps and patience.
I made the changes as you suggested and it worked very well.
Thanks again and have a great day,
Mike
mike tran
Greenhorn
Joined: Jul 10, 2012
Posts: 8
posted
0
Hello,
Could i ask you a quick question on the multipart/form-data in my form ?
Supposely, i have a field in my form namely student_name where student could enter his/her name, and student_name is a text field.
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
The problem which I encounter in my servlet that I could not retrieve student_name by doing the request.getParameter ("student_name"). It always returned a null pointer value.
Would you please give me some advice on how to retrieve this text field. I read from google and thet suggest using the mutipartrequest.getparameter values but it would be great if you can guide me to an example on how to use this.
I will post you an edited version of code from my file-upload servlet which uses request parameters. However I should say that everything I know about file uploads, I learned from examples on Apache's site. You should have a look there.
mike tran
Greenhorn
Joined: Jul 10, 2012
Posts: 8
posted
0
Thank you very much Paul for all of your help and supports.
It worked out beautifully.