controller

Ads

 
 

Share on Google+Share on Google+

meena n
controller
1 Answer(s)      5 years and 9 months ago
Posted in : Java Interview Questions
package com.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beans.SampleBean;
import com.dao.*;
/**
 * Servlet implementation class Controller
 */
public class Controller extends HttpServlet {
    private static final long serialVersionUID = 1L;
    String searchNameGlobal=null;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Controller() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        if(request.getParameter("page").equalsIgnoreCase("create"))
    {
        SampleBean sample=new SampleBean();
        sample.setFname(request.getParameter("fname")); 
        sample.setLname(request.getParameter("lname"));
        sample.setAge(Integer.parseInt(request.getParameter("age")));
        sample.setGender(request.getParameter("gender"));
        sample.setPhone(request.getParameter("phone"));
        DbAccess newAccess=new DbAccess();
        try
        {
            newAccess.createStudent(sample);
        }
        catch(Exception e)
        {
            System.out.println("Error in controller create");
        }

    }
    else if(request.getParameter("page").equalsIgnoreCase("view"))
    {
        SampleBean sampleView=new SampleBean();
        DbAccess dbAccess=new DbAccess();
        try
        {
            String searchName=request.getParameter("fname");

            sampleView=dbAccess.viewStudent(searchName);
            request.setAttribute("result", sampleView);
        }
        catch(Exception e)
        {
            System.out.println("Error in view controller");
        }
        RequestDispatcher rd=request.getRequestDispatcher("/jsp/ViewDetails.jsp");
        rd.forward(request,response);
    }
    else if(request.getParameter("page").equalsIgnoreCase("update"))
    {
        SampleBean sampleView=new SampleBean();
        DbAccess dbAccess=new DbAccess();
        try
        {
            searchNameGlobal=request.getParameter("fname");

            sampleView=dbAccess.viewStudent(searchNameGlobal);
            request.setAttribute("result", sampleView);
        }
        catch(Exception e)
        {
            System.out.println("Error in update controller");
        }
        RequestDispatcher rd=request.getRequestDispatcher("/jsp/UpdateDetails.jsp");
        rd.forward(request,response);
    }
    else if(request.getParameter("page").equalsIgnoreCase("updatedetails"))
    {

        SampleBean sampleUpdate=new SampleBean();
        sampleUpdate.setFname(request.getParameter("fname")); 
        sampleUpdate.setLname(request.getParameter("lname"));
        sampleUpdate.setAge(Integer.parseInt(request.getParameter("age")));
        sampleUpdate.setGender(request.getParameter("gender"));
        sampleUpdate.setPhone(request.getParameter("phone"));
        DbAccess dbAccess=new DbAccess();
        try
        {

            dbAccess.updateStudent(sampleUpdate,searchNameGlobal);
        }
        catch(Exception e)
        {
            System.out.println("Error in update controller");
        }
    }
    else if(request.getParameter("page").equalsIgnoreCase("delete"))
    {
        DbAccess accessObject=new DbAccess();
        try
        {
            String deleteName=request.getParameter("fname");
            accessObject.deleteStudent(deleteName);
        }
        catch(Exception e)
        {
            System.out.println("Error in delete controller");
        }
    }
    }


}
Ads
View Answers

February 14, 2012 at 1:22 PM


import java.io.IOException; import java.util.ArrayList; import java.util.Iterator;

import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.ilp.otts.dao.awarddao; import com.ilp.otts.model.awardpojo; import com.ilp.otts.service.awardinterface;

/** * Servlet implementation class awardcontroller */ public class awardcontroller extends HttpServlet { static final Logger logg = Logger.getLogger( awardcontroller.class); awardpojo awd=new awardpojo(); awardinterface ob=new awarddao(); private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public awardcontroller() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    logg.info("welcome");
    String hide=request.getParameter("add");
    logg.info(hide);


    if(hide.equals("add"))
    {
        logg.info("add");
        String name=request.getParameter("award");
        String reason=request.getParameter("reason");
        int amount=Integer.parseInt(request.getParameter("amt"));
        int id=Integer.parseInt(request.getParameter("awid"));
        String desc=request.getParameter("desc");
        String category=request.getParameter("cat");

        logg.info(name+"\n"+reason+"\n"+amount+"\n"+id+"\n"+desc+"\n"+category);


        awd.setName(name);
        awd.setReason(reason);
         awd.setAmount(amount);
         awd.setId( id);
         awd.setDesc( desc) ;
         awd.setCategory(category);
        ob.add(awd);
        RequestDispatcher dispatcher = request.getRequestDispatcher("Awardmanagement/addSuccess.jsp");
        dispatcher.forward(request, response);

    }
    else if(hide.equals("delete"))
    {

        logg.info("delete");
        int ctr=0;
    int id1=Integer.parseInt(request.getParameter("awid1"));
    awd.setId(id1);
   int c= ob.del(awd,ctr);
     if(c==0)
     {
    logg.info("No such id");
    RequestDispatcher dispatcher = request.getRequestDispatcher("Awardmanagement/deleteFailure.jsp");
    dispatcher.forward(request, response);
     }
     else
     {
    RequestDispatcher dispatcher = request.getRequestDispatcher("Awardmanagement/deleteSuccess.jsp");
    dispatcher.forward(request, response);
    }
    }

    else if(hide.equals("view"))
    {

        ArrayList<awardpojo> al=ob.view();
        logg.info("return");
        //Iterator ir=al.iterator();
        for(int i = 0; i < al.size(); i++)
        {
            awardpojo t = al.get(i);
                logg.info(t.getAmount() + "\t" + t.getName());
        }
        request.setAttribute("sss",al);
        RequestDispatcher dispatcher = request.getRequestDispatcher("Awardmanagement/awardsView.jsp");
            dispatcher.forward(request, response);

    }

    else if(hide.equals("update"))
    {
        logg.info("update");
        int id1=Integer.parseInt(request.getParameter("awid"));
        awd.setId(id1);

        ArrayList<awardpojo> al=ob.update(awd);
        logg.info("return");
        request.setAttribute("sss",al);
        RequestDispatcher dispatcher = request.getRequestDispatcher("Awardmanagement/awardUpdate.jsp");

            dispatcher.forward(request, response);

    }
    else if(hide.equals("updateresult"))
    {
        logg.info("updateresult");
        String name=request.getParameter("nn");
        String reason=request.getParameter("rr");
        String category=request.getParameter("cc");
        String description=request.getParameter("dd");
        int amount=Integer.parseInt(request.getParameter("aa"));
        int id=Integer.parseInt(request.getParameter("ident"));
        logg.info(name);
        logg.info(id);
        logg.info(reason);
        logg.info(category);
        logg.info(description);
        logg.info(amount);   
        awd.setName(name);
        awd.setId(id);
        awd.setAmount(amount);
        awd.setCategory(category);
        awd.setDesc(description);
        awd.setReason(reason);


        ob.updateresult(awd);
        RequestDispatcher dispatcher = request.getRequestDispatcher("Awardmanagement/updateSuccess.jsp");
        dispatcher.forward(request, response);
    }

}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub





}

}


Ads









Related Tutorials/Questions & Answers:
controller
; import com.dao.*; /** * Servlet implementation class Controller */ public class Controller extends HttpServlet { private static final long serialVersionUID...#HttpServlet() */ public Controller() { super(); // TODO Auto
tabbar controller
tabbar controller  how change the tabbar controller and how we maintain the view controller of second tabbar controller
Advertisements
Controller in Spring
Controller in Spring  Hello Sir please help that how to write Controller in Spring web thanks in advance   Hi you can write the Spring Controller by extending the abstract class AbstractController or by using
Spring Controller
Spring Controller  How to handle form request coming on the Spring Controller ?   Hi Please write the controller as for handling form... the @Controller annotation before the class declaration
Filter/Controller
Filter/Controller  what is the difference b/w org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter and org.apache.struts2.dispatcher.FilterDispatcher. which of the above is really used as controller and which
front controller - JSP-Servlet
front controller  How to implement front controller in a web application
abstract wizard form controller
abstract wizard form controller  Hi In roseindia for abstract wizard form controller example contains half of the code. please send the remaing half of the code like controller classes
@Controller annotation for defining new controller
In this section, you will learn about @Controller annotation for defining new controllers
previous view controller
controller in UINavigationController?   Generally, UINavigationController... to go back to the previous view controller. See the example given below.. UINavigationController But in case if you have more then one root view controller
Iphone Tab Bar Controller
Iphone Tab Bar Controller  Hi, I am very new in mobile application programming. So, how can i create iphone tab bar controller apps. Please suggest me any online help, so that i will enhance my technical skill in mobile web
JQuery as validation controller
JQuery as validation controller  I would love to use JQuery as validation controller but i don't know how i use.Could u help me?   Here is a link that will provide you an example of validating email using jquery
why servlet as controller - Struts
why servlet as controller   Hi Friends, Main reasons for using servlet as a controller in struts. why not jsp . Thanks Prakash  Hi... to generate the proper user response. So Servlet is used as controller. Thanks
Version of com.kennycason>gdx-controller dependency
List of Version of com.kennycason>gdx-controller dependency
Version of com.sdl>odata_controller dependency
List of Version of com.sdl>odata_controller dependency
Version of com.shipdream>controller-retrofit dependency
List of Version of com.shipdream>controller-retrofit dependency
Controller as jsp in mvc2? - Java Beginners
Controller as jsp in mvc2?   Hi Friend, I am beginner in web application ,plz answer my questions 1) can we use JSP as a controller in MVC-2 architecture. If so wat's d
Simple Form Controller Example
Example of using Simple Form Controller Page 2 In this page we... it at Example of using Simple Form Controller Step 7: Now we... then validator call by the controller and display errors like: If user fill
spring controller V/S stuts Action - Spring
spring controller V/S stuts Action  we are going to use spring framework so what is better spring controller or struts action
Version of com.shipdream>android-mvc-controller dependency
List of Version of com.shipdream>android-mvc-controller dependency
@Controller Annotation Example in Spring 3
@Controller Annotation Example in Spring 3: In this section we will see how we can create annoted controller class in Spring 3. You can @Controller annotation in the Java class to make it controller. In this tutorial we are creating
Maven Dependency gdx-controller >> 1.0
You should include the dependency code given in this page to add Maven Dependency of com.kennycason >> gdx-controller version1.0 in your project
Maven Dependency odata_controller >> 2.0.5
You should include the dependency code given in this page to add Maven Dependency of com.sdl >> odata_controller version2.0.5 in your project
Maven Dependency odata_controller >> 2.1.1
You should include the dependency code given in this page to add Maven Dependency of com.sdl >> odata_controller version2.1.1 in your project
Maven Dependency odata_controller >> 2.1.2
You should include the dependency code given in this page to add Maven Dependency of com.sdl >> odata_controller version2.1.2 in your project
Maven Dependency odata_controller >> 2.1.5
You should include the dependency code given in this page to add Maven Dependency of com.sdl >> odata_controller version2.1.5 in your project
Maven Dependency odata_controller >> 2.2.0
You should include the dependency code given in this page to add Maven Dependency of com.sdl >> odata_controller version2.2.0 in your project
Maven Dependency odata_controller >> 2.2.3
You should include the dependency code given in this page to add Maven Dependency of com.sdl >> odata_controller version2.2.3 in your project
Maven Dependency odata_controller >> 2.3.0
You should include the dependency code given in this page to add Maven Dependency of com.sdl >> odata_controller version2.3.0 in your project
Maven Dependency odata_controller >> 2.3.2
You should include the dependency code given in this page to add Maven Dependency of com.sdl >> odata_controller version2.3.2 in your project
Maven Dependency odata_controller >> 2.3.3
You should include the dependency code given in this page to add Maven Dependency of com.sdl >> odata_controller version2.3.3 in your project
Maven Dependency odata_controller >> 2.3.5
You should include the dependency code given in this page to add Maven Dependency of com.sdl >> odata_controller version2.3.5 in your project

Ads

 
Advertisement null

Ads