Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

New to servlets

 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class HelloWorld extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

res.setContentType("text/html");
PrintWriter out = res.getWriter();



}
}


What does those to highlighted line mean...
Please explain...
 
rakesh sugirtharaj
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. So lets imagine your browser as a File. So what do you do to write a file? You get a writer. The same here. Here we tell the browser before hand that "Hey Browsie! I m going to write on you and if you were just wondering what I m going to write - Its text in html format. Treat them as the same." Makes sense?

[ November 07, 2008: Message edited by: rakesh sugirtharaj ]
[ November 07, 2008: Message edited by: rakesh sugirtharaj ]
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whio is tell to whom ???
Client to server
or
server to client???

If server is telling to client, then it creates a doubt in me, that is...
It is the client who resquested so client know what type of data is coming.
then why is server again telling the same thing??

SORRY if i sound stupid
 
rakesh sugirtharaj
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whio is tell to whom ???
Client to server
or
server to client???

Ofcourse, the server to client. The problem is your opinion of client is human. Sure I know I m looking for a excel sheet. But does my 'browser' know?
SORRY if i sound stupid
You have no idea how stupid i was(and still am ) when i was a beginner! Just dont stop being curious because you think its stupid.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException


So what are the details does the object req send to the server???
 
rakesh sugirtharaj
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you go - the API docs
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is the client who resquested so client know what type of data is coming.
then why is server again telling the same thing??


Its not the case always. The client only sends requests for a resource and the server decides what to serve back.
For instance consider a online form filling/billing a application. The moment you fill in the details and click Submit, a PDF version of the completed form or a receipt is sent back to you.

The content type also needs to be set so that the browser knows what to do with the content. Browsers like Firefox can be customized to deal with particular content types like open the file with an associated application or save it ,etc.
Setting the content types helps the browser with this also.


It gets a writer to the output stream of the response object, using which data can be written to the response.

Hope this helps
[ November 07, 2008: Message edited by: Amit Ghorpade ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic