Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Servlets and the fly likes question on using servlet parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "question on using servlet parameters" Watch "question on using servlet parameters" New topic
Author

question on using servlet parameters

michael echavez
Greenhorn

Joined: Feb 07, 2005
Posts: 18
Hi! i've been recently studying about servlets and jsps. I've been reading this book entitled "mastering jakarta struts" and one of the examples there is the ParameterServlet.java. I've tried previous examples written in the book and they worked perfectly and then this one came - and I cant seem to make it work.

the problem is that when Form.jsp calls for the ParameterServlet, the browser wouldnt run the servlet and instead it tries to download the file saying that i'm trying to open a PARAMETERSERVLET FILE.

Can someone please help me? Below are excerpts from my code.

ParameterServlet.java
-----
package chapter2;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class ParameterServlet extends HttpServlet {

public void init(ServletConfig config) throws ServletException {
// Always pas the ServletConfig object to the super class
super.init(config);
}

// Process the HTTP Get request
public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}

// Process the HTTP Post request
public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("html/text");
PrintWriter out = response.getWriter();

out.println("<html>");
out.println("<head>");
out.println("<title>Parameter Servlet</title>");
out.println("</head>");
out.println("<body>");

// Get an enumeration of the parameter names
Enumeration parameters = request.getParameterNames();

String param = null;

// Iterate over the parameter names,
// getting the parameters values
while(parameters.hasMoreElements()) {
param = (String)parameters.nextElement();
out.println(param + " : " + request.getParameter(param) + "<BR>");
}

out.println("</body></html>");
out.close();
}
}
-----
Form.jsp
<HTML>
<HEAD>
<TITLE>
Parameter Servlet Form
</TITLE>
</HEAD>
<BODY>

<form
action="servlet/chapter2.ParameterServlet"
method=POST>
<table width="400" border="0" cellspacing="0">
<tr>
<td>Name: </td>
<td>
<input type="text"
name="name"
size="20"
maxlength="20"
</td>
<td>SSN:</td>
<td>
<input type="text" name="ssn" size="11" maxlength="11">
</td>
</tr>
<tr>
<td>Age:</td>
<td>
<input type="text" name="age" size="3" maxlength="3">
</td>
<td>email:</td>
<td>
<input type="text"
name="email"
size="30"
maxlength="30">
</td>
</tr>
<tr>
<td> </td>
<td>  </td>
<td>  </td>
<td>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Reset">
</td>
</tr>
</table>
</FORM>

</BODY>
</HTML>
-----
here is also a part of my web.xml
<servlet>
<servlet-name>ParameterServlet</servlet-name>
<description>
This servlet retrieves the parameters sent to it and returns
the parameters and their values back to the client.
</description>
<servlet-class>chapter2.ParameterServlet</servlet-class>
<!-- Load this servlet at server startup time -->
<load-on-startup>5</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>ParameterServlet</servlet-name>
<url-pattern>/servlet/chapter2.ParameterServlet</url-pattern>
</servlet-mapping>
-----

I've put the files on the right directories since i've got other servlets under the chapter2 folder and they work correctly. Also with the web.xml, i now that i must define all the servlet tags before the servlet-mapping tags so there is no problem there. Now i am completely lost since i dont know why the application does not work
Rusty Enisin
Ranch Hand

Joined: May 26, 2005
Posts: 107
The content type is getting set wrong. It should be set to "text/html".

// Process the HTTP Post request
public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("html/text");


This should read:



Hope this helps
[ June 09, 2005: Message edited by: Scott Enisin ]

The squeaky wheel gets the grease. Well, that or replaced...
michael echavez
Greenhorn

Joined: Feb 07, 2005
Posts: 18
Hello Scott,

I'm guessing that the content type setting must also be the problem. I did the code all over again yesterday and it worked - not knowing what could have been the problem. Now that you pointed that one out, I think that it was the problem. I'll be more careful with the content setting next time. Thank you so much Scott! Good day!

Regards,
Michael
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question on using servlet parameters
 
Similar Threads
Value in dropdown from servlet
two dropdown menus
Folder Creation On Server
regarding servlet mapping
Servlet Problems