Hello,

I'm trying to understand a very simple example of using html + servlets using Eclipse EE.

I have an index.html :

<!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>Ze title!</title>
</head>

<body>

	<form action="MyServlet" method = post>
		<input type= text name = text>
		<input type = submit value="Submit">
	</form> 

</body>
</html>

And a servlet named MyServlet:

package test;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class MyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public MyServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String s=(String) request.getParameter("text");
		
	
		out.print("<html>");
		out.print("<head><title>Added title</title></head>");
		out.print("<body>");
		

		out.print("input= "+s+"  this is the GET method");
		out.print("</body></html>");
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String s=(String) request.getAttribute("text");
		
		out.print("input= "+s+"  this is the POST method");
	}
   
}

So I have a doGet and a doPost method. If I use:

<form action="MyServlet" method = post>

in my index.html, the Eclipse browser still executes the GET method, while Opera uses the POST method. Why is this?

Furthermore, can I add more methods in my servlet and call them from my html?

<form action="MyServlet" method = FooMethod>

Now that I've put it in writing, it does look pointless. In the tutorials I've found, I've only seen adding more methods but calling them from within the GET/POST methods, not directly.


Thanks.

Recommended Answers

All 8 Replies

Depending on DOCTYPE, Eclipse has the right to be picky over badly formed HTML.

Assuming XHTML, attributes should be in "double quotes".

This may fix it:

<form action="MyServlet" method="post">

Airshow

Doesn't matter.

Buffalo,

It was just a thought.

I can't immediately see how doGet/doPost is called - presumably in HttpServlet. Can you intercept the logic and see where it breaks down?

Airshow

Sorry for sounding rude, Airshow.


Well, my problem with running the doGet / doPost methods is still here. I'll quote something I found about the two methods:

1-->

In doget() method there is a security problems are arrival because when the user or client submit the form the associated user name or password has been display on the addressbar.

but in case of doPost() method when the user submit the form the associated user name or password are not display on the addressbar.

2-->

by doget() method client can send limited amount of data.

but in case of dopost() method client can send more then data as compare to doget() method.

doGet is used when there is are requirement of sending dataappended to a query string in the URL. The doGet models the GET methodof Http and it is used to retrieve the info on the client from some server as arequest to it.The doGet cannot be used to send too much info appended as a querystream. GET puts the form values into the URL string. GET is limited toabout 256 characters (usually a browser limitation) and creates reallyugly URLs.

POST allows you to have extremely dense forms and pass that to theserver without clutter or limitation in size. e.g. you obviously can'tsend a file from the client to the server via GET.

POST has no limit on the amount of data you can send and because thedata does not show up on the URL you can send passwords. But this doesnot mean that POST is truly secure. For real security you have to lookinto encryption which is an entirely different topic

Depending on the request type (get or post) container invokes the corresponding doGet() or doPost() method by default the request type is get and the container looks for doGet() method.

preferabily for posting data through form post is used and for getting data from server side preferabily get is used get is fastt than post request get transfers data through url and hence not safe post transfer data as a body or in the form of packets get can transfer limited data post has no such limitation.

Well, okay. But if I request the POST function in my HTML file, why does GET run?

<form action="MyServlet" method = post>
		<input type= text name = text>
		<input type = submit value="Submit">
	</form>

If anyone has a good example of how the two methods are used, please link me up.


Thank you.

Buffalo,

My guess is still that a GET request is being issued despite METHOD = post . In other words the problem is with Eclipse not the servlet.

Looking through the eclipse.com/forums a number of form submission problems seem to arise.

Can you enquire your server logs to inspect the request?
Can you try a different browser (FF, IE, Opera etc) to submit the form? (If necessary create a separate test page with hard-coded absolute action="http://......" )

Airshow

My only conclusion is that the Eclipse browser is retarded.

Opera and IE perform normally, while the Eclipse browser fails at everything.

For instance, I modified my HTML to have this:

<form action="MyServlet" method = "get">

		<input type= text name = text><br>
		<input type= text name = name><br>
		<input type = submit value="Submit">

	</form>

You would expect Eclipse to display a form with 2 fields, like Opera and IE. But that's not the case. I think I'll just forget about it and use a normal browser.

[IMG]http://www.picz.ro/thumbnails/5883e7fbb6c603f78469850e149bc643.png[/IMG]

[IMG]http://www.picz.ro/thumbnails/a57613731cc7b9ade9facccf72251c55.png[/IMG]

[IMG]http://www.picz.ro/thumbnails/5b9f179a0f9dcbdd1f69438b99c6f639.png[/IMG]

Turns out, the Eclipse browser DOES work for a brief time after closing & opening it again. Stopping the server, saving all the files, publishing etc - nothing worked before.

Since I have now cleared the initial problems stated, I am marking this as solved. Thanks Airshow.

Well done Buff. I'll remember all that in case I need to go there myself one day.

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.