Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


meena n
controller
1 Answer(s)      2 years and 7 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");
        }
    }
    }


}

Advertisement
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





}

}



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
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
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
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
@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
Understanding Struts Controller
Understanding Struts Controller       In this section I will describe you the Controller.... It is the Controller part of the Struts Framework. ActionServlet is configured
Why Controller in j2ee - Servlet Interview Questions
Why Controller in j2ee  Hi, Why controller(servlet) used in j2ee web application.what happens if no controller. Thank u in advance  Hi Friend, In MVC framework,the role of controller is very important.It centralizes
How to pass the value from controller to jsp page ?
How to pass the value from controller to jsp page ?  HI I need to pass the value from controller to jsp. I use the way like this Controller.... when i set the controller as request.getSession().setattribute("msg
Accessing Jqxgrid data in Controller layer using JSP
Accessing Jqxgrid data in Controller layer using JSP  I am using... controller layer using JAVA. What i actually require is to fetch the data which... to Controller layer,thats more than enough, I can take it forward. Is there any
Y servlet as a controller - Servlet Interview Questions
Y servlet as a controller   Hi Friends, Can anyone tell me why servlet used as controller not jsp. What are the advantages.  Hi Friend... as it is having pure java code. Therfore servlet is used as a controller. To learn
How to use spring validator with multiaction controller?
How to use spring validator with multiaction controller?  Hi, I am trying to use spring validator with multiaction controller. The configuration...; And the controller has method called update(..., BindException error) ----it shows
iPhone Tab Bar with Segmented Controller
to use the two controller at the same time, Tab bar is used to show the different view on click but in segmented controller you cannot recognize that its... and on that will add Tab bar controller and in that tab bar controller view
iPhone Segmented Controller Through Coding
iPhone Segmented Controller Through Coding In this Tutorial will learn how to create Segmented Controller through coding and also how to set on to the view... Controller which we are using in our application is using Interface Builder but here
Controller Interface example in Spring 2.5 MVC. Learn how to create and run the example.
Controller Interface       Controller Interface implementation example in Spring 2.5 Web MVC framework:- In this section we will see the example of Controller Interface
Null value when i set interface object in form controller - Spring
Null value when i set interface object in form controller   Hi all... interface object in form Controller. public void setOHBillManager(OHBillManager.... when i call this method in form controller ohbillManager.saveBill(ohbill); i
How to write the junit test code for the following controller code
How to write the junit test code for the following controller code  //Controller Code package com.payoda.springs; import java.util.ArrayList... java.util.List; import java.util.Map; @Controller public class StartController
How to get the browsed file path into controller bean using JSF icefaces
How to get the browsed file path into controller bean using JSF icefaces  Hi All, I've a requirement as, 1) Need to browse a file from my local system/machine 2) Path of the file from which I browsed need to be stored
Pass message controller to viiew with the help of Model in Spring 3
Pass message from Controller to View with help of Model in Spring 3.0... from controller to the view layer. For example, if the class or view validator not include in the controller class in the spring 3.0 application and you want
Spring MVC Controller hierarchy
. The main components involved are DispatcherServlet, Controller and Views... request and delegates it with Controller. Following diagram shows the very... as the controller and it delegates the request to the Controller. Developers
Tab Bar Controller
Tab Bar Controller In this Tutorial, you will learn to       a)  Create a Tab Bar Controller   ...; c)  Adding the Tab Bar Controller to the window. In this application
Push View Controller - Example
Controller - (void)pushViewController:(UIViewController *)viewController... and that object neither can be an instance of tab bar controller nor on the navigation... { //Navigation logic may go here. Create and push another view controller
net.roseindia.controller
This package contains the controller class of the sample application
Java Model View Controller (MVC) Design Pattern
View Controller ) Design Pattern Model View controller is a classical design... of data, which manipulated by the controller. 2. View- The view displays the data.... It is actually representation of model data. 3. Controller- The controller
net.roseindia.controller
This package contains controller class of Spring and Hibernate Jpa integration application
iPhone Segments Changes ToolBar And ToolBar Button
how to use the Segmented Controller to change the ToolBar Style and also by another Segmented Controller will change the ToolBar Button Style, this is View... variable for the Segmented Controller and the Toolbar after declaring variable
ZF Core Components
of MVC (Model-View-Controller). In ZF there are three classes make the MVC system Zend_Controller (Controller), Zend_Db (Model), Zend_View (View). Many... as well as to which controller the control should be handed over etc.  
Interview Questions - Struts Interview Questions
is RequestProcessor and RequestDispatcher? Answer:  The controller is responsible.... The controller is responsible for selecting the next view based on user input and the outcome of model operations. The Controller receives the request from
JSP Controls Tag Library
JSP Controls Tag Library       JSP Controls Tag Library provides the lifecycle for portlet-like JSP components. The Library does not require a portal engine or other central controller
What is Struts
and Model-View-Controller (MVC) Architecture. Struts has a set of tag libaries... elegant solutions to difficult problems. Model-View-Controller: View... to the Controller i.e. Java Beans. Controller takes the information submitted to the Model
RequestDispatcher vs sendRedirect
RequestDispatcher vs sendRedirect       A Controller servlet can conclude either a forward or a redirect operation at the end of processing a request. Here are the basic differences
PHP Frameworks
PHP Frameworks       Zephyr Zephyr is an MVC (model-view-controller) framework..., Association Data Mapping, Front Controller and MVC.   
Spring 3 MVC Registration Form Example
Controller and another related files. Example discussed here will present user...; } } Step 3: Now create  Controller class "... org.springframework.web.bind.annotation.RequestMethod; import net.roseindia.form.Registration; @Controller
Open Source Web Frameworks in Java
is the implementation of Model-View-Controller (MVC) design pattern for the JSP...-View-Controller (aka "Model 2") framework for web publishing using Java and J2EE... to represent the model and the controller and templates use this components to render
Struts Tutorial
to develop Java web applications. Based on the Model-View-Controller (MVC..., as well as Velocity Templates, XSLT. Controller: Controller in struts framework..., Controller Architecture and Struts 2 in simple and easy examples. Moreover
MVC Architecture In Java
, what is MVC architecture, what is model, what is view, what is controller...-Controller) design pattern. What is MVC Architecture ? Models designs on MVC... into three parts namely Model, View, and Controller. MVC design pattern is used
Segment application
iPhone Segment application   In this application we are going to create iphone segment application by using View based application. In this we are using Segmented Controller and will allot different view to different segment
Spring 3 MVC Login Form Example
create the Controller class "LoginController.java"  under src... net.roseindia.form.LoginForm; @Controller @RequestMapping("loginform.html
Jakarta Struts Interview Questions
;    Q: What is Jakarta Struts Framework? A: Jakarta Struts is open source implementation of MVC (Model-View-Controller... applications with Java. Q: What is ActionServlet? A: The class
 

 

 

DMCA.com