aspose file tools*
The moose likes IDEs, Version Control and other tools and the fly likes help on first servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "help on first servlet" Watch "help on first servlet" New topic
Author

help on first servlet

mike tran
Greenhorn

Joined: Jul 10, 2012
Posts: 8
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
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

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... ]


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
mike tran
Greenhorn

Joined: Jul 10, 2012
Posts: 8
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

Where did you put the servlet class file?
mike tran
Greenhorn

Joined: Jul 10, 2012
Posts: 8
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.




[Thumbnail for dung.jpg]

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

I've moved this to the IDEs forum. I don't use Eclipse and don't know what it does with your class files.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18662
    
    8

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
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
Bartender

Joined: Oct 14, 2005
Posts: 18662
    
    8

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
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
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
Bartender

Joined: Oct 14, 2005
Posts: 18662
    
    8

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
Thank you very much Paul for all of your help and supports.
It worked out beautifully.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: help on first servlet