• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help on first servlet

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

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.


Also my test servlet code, and my web.xml code



my web.xml code





Thank you very much in advance
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

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().

And can you please UseAMeaningfulSubjectLine next time? I've modified it for you this time.
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The form action should be the context path followed by the servlet path. As in:



assuming the form is in a JSP. If the form is in an HTML page, you need to hard-code the context path.

[Edit: Rob snuck in ahead of me... ]
 
mike tran
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 tried all those . but the error is still there.

I wonder if you could help.

Thank you very much.


 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where did you put the servlet class file?
 
mike tran
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Thanks for all of your helps.

dung.jpg
[Thumbnail for dung.jpg]
File structure of my test application
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've moved this to the IDEs forum. I don't use Eclipse and don't know what it does with your class files.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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





so I don't know why it is complaining about it.


Please helkp me if you can.

Thank you very much
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 />

<form action="${pageContext.request.contextPath}/mytest" method="post" enctype="multipart/form-data">

<input type="student_name" name="text" size="50" />
<br />

<input type="file" name="file" size="50" />
<br />

<input type="file" name="file" size="50" />
<br />

<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>



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 appreciate your helps very much.
Mike

 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Paul for all of your help and supports.
It worked out beautifully.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic