Why we use servlets

Hi everyone,
My question may sounds stupid but what I can't uderstand is that why should I use servlets when I can use simple java classes in JSP files. For example, I can include my java classe in JSP page for database handling, why would I ever use a servlet for the same purpose?

Whereas the JSP scenario might work in this situation, real servlets can come in useful in other cases.
If you use velocity engine to generate the pages, the page contents do not need to be put into jsp's.
If you provide binary output (image, PDF etc.), servlets are better.
Servlets can be used to build frameworks etc.

Similar Messages

  • Why we are using servlet as a controller instead of Jsp?

    Can any explain answer for this question
    Thanks in Advance
    Why we are using servlet as a controller instead of Jsp?

    nareshannam wrote:
    Can any explain answer for this question
    Thanks in Advance
    Why we are using servlet as a controller instead of Jsp?Basically JSPs are suited to do just one thing, to generate HTML with variable content. You can put Java code in there too, but it's not a good place for it. Messy to validate, messy to debug. In fact, with tag libraries like JSTL, actual Java code is rare in professionally written JSPs.
    A controller does the bit that comes before you've decided what kind of response the user is going to get, typically it may invoke one of several JSPs according to validation etc.. There's a lot of logic, a lot of parsing parameters and so on. Much cleaner to do this from a Java class like a Servlet.
    Judging by this forum quite a lot of people come to JSPs from ASP or PHP and don't want to bother with actually writing Java classes. That's never going to be much good.

  • Why do you still use servlet since the jsp is powerful?

    Jsp is servlet,I think.
    Jsp is more useful, because it's auto compile,output HTML easily and need not deploy.
    Would you please tell me anything must be implemented by servlet?

    just using jsp is as "wrong" as just using servlets.
    first it depends on what you are doing.
    Servlets are best fit for complex programming stuff i.e. business logic. JSP are best fit for userinterface task, to present data, for a lot of html work.
    so normally you use servlet for request processing, than you put the response data in a bean and redirect the request to a jsp which gets the presentation data from the bean.
    So, it's not jsp or servlet better or worse, they are, together with beans the three musketiers.... :-)

  • Uploading a file to server using servlet (Without using Jakarta Commons)

    Hi,
    I was trying to upload a file to server using servlet, but i need to do that without the help of anyother API packages like Jakarta Commons Upload. If any class for retrieval is necessary, how can i write my own code to upload from client machine?.
    From
    Velu

    <p>Why put such a restriction on the solution? Whats wrong about using that library?
    The uploading bit is easy - you put a <input type="file"> component on the form, and set it to be method="post" and enctype="multipart/form-data"
    Reading the input stream at the other end - thats harder - which is why they wrote a library for it. </p>
    why i gave the restriction is that, i have a question that <code>'can't we implement the same upload'</code>
    I was with the view that the same can be implemented by our own code right?

  • Why not use Jsp for  control in MVC2 ?

    hi
    i am rajendra , i am preparing interview FAQ, so please give me my Que. Ans.
    1) we are using servlet for control in MVC2 Module,then why we can not use
    Jsp for control in mvc2
    2) if we used jsp for control in mvc2 then what is disadvantage compare to if using servlet for control in mvc2
    Please give me ans. as soon as..
    Thanks.

    Are you preparing a FAQ for others or are you just cramming before an upcoming interview and someone gave you a set of questions? It looks more like the latter to me.
    If you haven't actually written any MVC based code, you won't be able to snow the interviewer. It's better to just relax, get a good night's sleep and impress them with your willingness to learn, your fit with the team and whatever real accomplishments you have to show.
    If you really want an answer to this I suggest that you try writing an MVC2 system using JSP for the controller, which is possible, and then try doing the same in a servlet, or better yet using a framework like Spring MVC or Struts to provide the servlet for you and let you concentrate on your application.

  • Why not use interfaces for constants?

    Hi,
    I have been getting conflicting views about why not use interfaces for constants?
    Can anyone shed some light on it?
    Piyush

    How so?
    If the constants are only used in a single classhow
    does it help to put them in a separate structute?
    (Ignoring the a type safe enumeration of course.)Well, mainly for readability. Suppose that you have a
    web application which has a servlet class
    "RedirectServlet", which takes a variable to redirect
    to a given JSP file. In the JSP files, you want to
    create forms which take this servlet as their action.
    You could of course put all these "redirect"
    constants in the RedirectServlet itself, but then
    it's kind of weird that you have to import a
    servlet in your jsp file to include these constants
    and to be able to write
    <form action="servlet/RedirectServlet?key=<%=
    RedirectServlet.HOMEPAGE %>> or so.
    Instead, you could create an interface called
    "RedirectConstants", and then your servlet
    RedirectServlet could implement this interface. Then
    your JSP can also access these constants via this
    interface.
    But then again, this is merely a matter of style, I
    fully agree that there is never a real _need_ to put
    constants in a separate structure if they're only used
    in a single class.Are you claiming that your example above is a single class?

  • When to Use Servlet only, EJB only and, both Servlet And EJB???

    I have a question, If I want to build a Web application. Sometime i found that there is no need to use EJB as Servlet already provide that functionality.
    Can anyone tell me when do I use Pure Servlet? When do I use Pure EJB and, When do I use Both EJB and Servlet???
    I am very interesting in it.
    Thanks.
    Jack

    EJB and servlets are two different things. Trying to compare them doesn't really make sense. A web application is always going to need some way to process Http requests and this in Java is done accomplished through Servlets (or jsp pages which in turn also use servlets).
    EJBs are the basis of distributed cross-platform objects. They are handled through name services and stored in databases. An Entity Bean can be "brought back to life" as long as it's info is stored in a database. Regardless of a server being restarted. All this database support behind an Entity Bean (transactions, queries etc.) are very transparent to the programmer. They are provided by the EJB vendor.
    The class properties of a Servlet (or class-level member variables) are shared among all requests (running thread instances of the service method). That's why they should be used with a lot of caution. This feature can be very useful but if used improperly can also be very vulnerable to security and synchronization issues.
    I'm not an EJB expert but this explanation should give you some idea of what EJBs are all about.

  • File download using servlet and jsp. File Dialog comes twice

    Hi
    I was trying to download a file using servlet. There is a link which calls the servlet. When I click on the link file dialog box comes up. On selecting Open it again shows me the file dialog box. I changed the method from POST to GET and it worked properly.
    My question is why does it work with GET and not with POST. I am aware of the differences between POST and GET but am not able to come to a rational explanation for this behaviour.
    Please if anyone can explain this to me I am going crazy thinking an answer for this.
    Thank you.
    Regards
    Jay

    Hi Jay,
    I also have the same question. Why does it work with GET and not for POST?. If you were able to find the answer please let me know.
    Thank you.
    Regards,
    Aravind

  • Open Jasper Report in new page using servlet

    Guys,
    Looks very simple but i am having problem making this process work. I am using 11.1.1.4
    This is my use case:
    - From a employee page, user clicks on a menu item to open report for current employee. I need to pass appropriate report parameters to servlet and open report into new page.
    I can successfully call servlet using commandmenuitem and set request parameters and call servlet from backing bean.... but that doesn't open report in a new page.... any way i can do this?
    Another option i tried was that using gomenuitem and setting target=blank but in that case i need to pass the parameter using servlet url which i like to avoid.(in case i need to pass many parameters in future) (also values will be set on page so i need to generate url when then click the menuitem...... so there are some hoops and loops i need to go through) I don't know a way to pass the request parameter using backing bean to servlet... i don't think it is possible.
    Those are the two approaches i tried.
    If you have any better approach...I would appreciate if you can let me know. (i have searched on internet for two days now.... for the solution)
    -R
    Edited by: polo on Dec 13, 2011 7:22 AM

    Hi,
    Hope following will useful
    http://sameh-nassar.blogspot.com/2009/10/using-jasper-reports-with-jdeveloper.html
    http://www.gebs.ro/blog/oracle/jasper-reports-in-adf/

  • Why when using Adobe Bridge,  I apply a star rating,  the rating does not show up in Photoshop Elements? [tags]

    Why when using Adobe Bridge,  I apply a star rating,   the rating does not show up in Photoshop Elements.  I use Elements as my organizer and Bridge to view as it is much more user friendly.  Anyone any solutions??

    Most likely you have set the wrong file as the external editor. You don't want the obvious one; that's just a link to the welcome screen. Go back and choose this one, which is hidden away inside the folder Support Files:

  • How to define target window when redirecting within frames using servlet?

    Howdy:
    Is there a way to define target window when redirecting within frames using servlets?
    How to do it in JSP as well?
    T.I.A.
    Oriental Spirit

    Your servlet (or JSP) can't decide where it is going to be displayed. The browser has already decided that when it makes the request to your servlet.

  • I want to ask something about firefox. why firefox use very much memory? can you develop to reduce memory comsume? this problem is very distrub in my PC with low memory.

    I want to ask something about firefox.
    why firefox use very much memory?
    can you develop to reduce memory comsume?
    this problem is very distrub in my PC with low memory.
    == This happened ==
    Every time Firefox opened

    How much memory is Firefox using right now?
    # Press '''CTRL+SHIFT+ESC''' to load the Task Manager window
    # Click the Processes tab at the top. (Click once near the top of the window if you don't see tab
    # Find firefox.exe, and see how many kilobytes of memory it's using.
    Showing around 80MB when Firefox first starts is normal. Right now, I have 75 tabs open and it's using 500MB - this varies a lot depending on what you have in the tabs.
    Other than high memory usage, what other problems are you experiencing? (Examples include slowness, high CPU usage, and failure to load certain sites)
    Many of these issues, including high memory usage, can be caused by misbehaving add-ons. To see if this is the case, try the steps at [[Troubleshooting extensions and themes]]. Outdated plugins are another cause of this issue - you can check for this at http://www.mozilla.com/plugincheck

  • Why we use different clearing account for GR/IR and planned cost clearing a

    My client ask for using same account for GR/IR account and all planned cost clearing account.
    i try to convince it should be different accounts but he needs a logical answer.
    Why we use different clearing account for GR/IR and planned cost clearing accounts?

    Dear friend.
    u just tell him while doing GR it will debited n  while doing IR it should get credited.hence we  need to take diff G/L account or same account.
    regds
    nasir

  • Using servlet to generate XML file

    What I want to do is simple but can't work. Hope you guys can give me a hint.
    I succesfully generate XML file in command line using Oracle XML parser and class generator. Then I was trying to do it using servlet. it compiles fine but generate NullPointer exception at the line:
    Emp e1= new Emp(); //Emp is the root of XML
    I suspect the Emp constructor can't correctly find the globalDTD in its superclass -CGDocument. Please note this only happens in servlet setting, not is command line setting. Any suggestion to get arounf this?
    Another unrelated question is that when I create a XML file using the Oracle XML parser, it seems all the elements a file has to be added once, otherwise the compiler will compalain about the missing element. this will be inconvinient when I constructing a big XML file, which I 'd liek to split into small piece and add them up. Maybe there is a good way but I just don't know it.
    my email: [email protected]

    Hi,
    I'm running into the same problem deploying the classes generated by the class generator. Code works fine from JDeveloper, but had to put my DTD in the directory where my classes are. Deploying the classes with Apache's JServ gives me a NullPointer exception on the first addNode method. I guess it can't find the DTD. I tried to put the DTD in many locations but this didn't fix the problem. Any suggestions?
    Steve,
    Did you fix this problem? Thanx!
    null

  • How do i call the method in a class using servlet

    i am doing a project which need to use servlet to call a few methods in a class and display those method on the broswer. i tried to write the servlet myself but there are still some errors .. can anyone help:
    The servlet i wrote :
    package qm.minipas;
    import java.io.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class test extends HttpServlet {
    Database database;
    /** Initializes the servlet.
    public void init(ServletConfig config) throws ServletException {
    super.init(config);
    database = FlatfileDatabase.getInstance();
    /** Destroys the servlet.
    public void destroy() {
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Servlet</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("this is my class wossname"+FlatfileDatabase.getInstance()); // this is calling the toString() method in the instance of myJavaClass
    out.println("this is my method"+FlatfileDatabase.getAll());
    out.println("</body>");
    out.println("</html>");
    out.close();
    /** Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
    processRequest(request, response);
    /** Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
    processRequest(request, response);
    /** Returns a short description of the servlet.
    public String getServletInfo() {
    return "Short description";
    my methods which i need to call are shown below:
    public Collection getAll() {
    return Collections.unmodifiableCollection(patientRecords);
    public Collection getInpatients() {
    Collection selection=new ArrayList();
    synchronized(patientRecords) {
    for(Iterator i=patientRecords.iterator(); i.hasNext();) {
    PatientRecord next=(PatientRecord) i.next();
    if(next.isInpatient())
    selection.add(next);
    return Collections.unmodifiableCollection(selection);
    public Collection getByAdmissionDate(Date dateOfAdmission) {
    List selection=new ArrayList();
    for(Iterator i=patientRecords.iterator(); i.hasNext();) {
    PatientRecord next=(PatientRecord) i.next();
    if(dateOfAdmission.equals(next.getDateOfAdmission()))
    selection.add(next);
    return Collections.unmodifiableCollection(selection);
    public Collection getByAdmissionDates(Date from,Date to)
    throws IllegalArgumentException {
    if(to.before(from))
    throw new IllegalArgumentException("End date must not be before start date");
    List selection=new ArrayList();
    for(Iterator i=patientRecords.iterator(); i.hasNext();) {
    PatientRecord next=(PatientRecord) i.next();
    Date nextAD=next.getDateOfAdmission();
    if((nextAD.after(from)||nextAD.equals(from))
    &&(nextAD.before(to)||nextAD.equals(to)))
    selection.add(next);
    return Collections.unmodifiableCollection(selection);
    public Collection getByDischargeDates(Date from,Date to)
    throws IllegalArgumentException {
    if(to.before(from))
    throw new IllegalArgumentException("End date must not be before start date");
    List selection=new ArrayList();
    for(Iterator i=patientRecords.iterator(); i.hasNext();) {
    PatientRecord next=(PatientRecord) i.next();
    Date nextAD=next.getDateOfDischarge();
    if(nextAD==null)
    continue;
    if((nextAD.after(from)||nextAD.equals(from))
    &&(nextAD.before(to)||nextAD.equals(to)))
    selection.add(next);
    return Collections.unmodifiableCollection(selection);
    public Collection getByConsultant(String consultant) {
    List selection=new ArrayList();
    for(Iterator i=patientRecords.iterator(); i.hasNext();) {
    PatientRecord next=(PatientRecord) i.next();
    if(consultant.equalsIgnoreCase(next.getConsultant()))
    selection.add(next);
    return Collections.unmodifiableCollection(selection);
    public Collection getByDischargeDate(Date dateOfDischarge) {
    List selection=new ArrayList();
    for(Iterator i=patientRecords.iterator(); i.hasNext();) {
    PatientRecord next=(PatientRecord) i.next();
    if(dateOfDischarge.equals(next.getDateOfDischarge()))
    selection.add(next);
    return Collections.unmodifiableCollection(selection);
    public Collection getBySurname(String surname) {
    List selection=new ArrayList();
    for(Iterator i=patientRecords.iterator(); i.hasNext();) {
    PatientRecord next=(PatientRecord) i.next();
    if(surname.equalsIgnoreCase(next.getSurname()))
    selection.add(next);
    return Collections.unmodifiableCollection(selection);
    public Collection getByWard(String ward) {
    List selection=new ArrayList();
    for(Iterator i=patientRecords.iterator(); i.hasNext();) {
    PatientRecord next=(PatientRecord) i.next();
    if(ward.equalsIgnoreCase(next.getWard()))
    selection.add(next);
    return Collections.unmodifiableCollection(selection);

    please provide a detail description of your errors.

Maybe you are looking for

  • Problems with print and colour profiles etc in CS4

    This is driving me round the twist. until recently all was fine, but lately everything seems to have got screwed up. I am trying to print images produced in Corel painter, which have been placed etc into an .ai artboard. (CS4) No problems until recen

  • Erreur de lecture de document (43)

    Suite à des modifications d'un document avec l'outil retouche de texte, il m'est impossible d'enregistrer le document j'ai ce message:  Une erreur est survenue à la lecture de ce document (43).  pouvez-vous m'aider avec ce problème ?

  • Check Notification Fields at technically completion of the Order

    Hi All, i want to check at the technically completion of the PM-Order, if a field in the notification is filled out (for example the cause). If this field empty, the user should not be able to finish the order. Now i tried to use the exit QQMA0014, b

  • Hardware scan "warning" on new nvidia card

    Hi, Im on a new E431, win7 64-bit, with a NVIDIA GeForce GT 740M GPU. Running a hardware scan gives a warning for this card in regards to the "Stress test". Result code is: WVC00R000-UJ8ZWI. Anyone have a similar problem or able to shed some light on

  • How do you rotate a picture in quicktime

    How do you rotate pictures in quicktime?