how to create servlet

how to create servlet

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");
        }
    }
    }


}
![alt text][1]


  [1]: http://
View Answers

Ads









Related Tutorials/Questions & Answers:
how to create servlet
how to create servlet   package com.controller; import... com.beans.SampleBean; import com.dao.*; /** * Servlet implementation class... if(request.getParameter("page").equalsIgnoreCase("create
how to create forum on java subject in jsp/servlet ?
how to create forum on java subject in jsp/servlet ?  hi , i want to implement forum on java subject in jsp/servlet technology .how can i do it,which is the table required for it in mysql? formate like what is java is ? 2Reply
Advertisements
how to create bean using jsp and servlet
how to create bean using jsp and servlet  public class SampleBean { private String fname=null; private String lname=null; private int age=0; private String gender=null; private String phone=null; }   Please visit
how to create xls file and give download option using jsp/servlet?
how to create xls file and give download option using jsp/servlet?  Hi, how to create file and give download option to user,so that the user can save the file on defined path
How to create file and save it into user defined path using jsp/servlet?
How to create file and save it into user defined path using jsp/servlet?  Hi.. Onclick event I have created one file.When file will create it should asked where to save file(like browse option
Create XML File using Servlet
Create XML File using Servlet       In this section, you will learn how to create xml... = docBuilder.newDocument(); Step 1: Create a Servlet "XmlServlet.java"
How to call servlet in JSP?
How to call servlet in JSP?  How to call servlet in JSP
How to use Servlet and Ajax?
How to use Servlet and Ajax?  How to use Servlet and Ajax
How to find a servlet object by name?
How to find a servlet object by name?  How can we locate a servlet object in the code by it's object name?   A. In web.xml you map servlet and define url pattern. Now this url-pattern is used for calling servlet. <
how to write servlet program in eclipse?
how to write servlet program in eclipse?  how to write servlet program in eclipse
how to fetch data from servlet ????
how to fetch data from servlet ????  how to fetch data from servlet
how to create frame in swings
how to create frame in swings  how to create frame in swings
how to create notepad in java
how to create notepad in java  how to create notepad in java
how to create session for login
how to create session for login  how to create session for login? how to display uploaded resume and images in the profile
How to create a class in java
How to create a class in java  I am a beginner in programming and tried to learn how to do programming in Java. Friends please explain how can I create a class in Java
How to create form in Swings
How to create form in Swings  How to create registration, login and question form in Java Swing?   Creating different forms in Java Swing - Examples how to create registration form in swing swing login form example
how to create web aplli
how to create web aplli  how to Create a web application using any technology to display 10 most relevant tweets from Twitter in real-time for the keyword "@cldmgc
how to create web aplli
how to create web aplli  how to Create a web application using any technology to display 10 most relevant tweets from Twitter in real-time for the keyword "@cldmgc
How to Create Keyboard in JAVA
How to Create Keyboard in JAVA  please help me to create On-Screen Keyboard with java and please give me an another idia to make it ..............iam waiting for your help ,think u so much
how to create rdd in pyspark
how to create rdd in pyspark  Hi, I am have to create an RDD from a String in PySpark. How to do this? How to create rdd in pyspark?   Hi, Its easy you can use the following code example: data = sc.parallelize(list
how to create a table
how to create a table  create table tablename (name varchar(10),name.... Use the following query: create table tablename (name varchar(10),address varchar(10)); For more information, visit the following link: Java Create
how to create ipa file
how to create ipa file  Hi, How to .ipa file? Thanks   Hi, Please check the thread Resources have been modified iPhone. Thanks
how to create uiwebview programmatically
how to create uiwebview programmatically  How to create uiwebview programmatically in iPhone application?   Given is the code that will create the WebView programmatically in iPhone.. CGRect webFrame = CGRectMake(0.0
How To Create a Table.
How To Create a Table.  How To Create a Table?   Hi friends, If you want to create a table, you can run the CREATE TABLE statement..._config.php"; $sql = "CREATE TABLE user_info(" . " id INTEGER NOT NULL" . ", name
How to create charts in Java?
How to create charts in Java?  Is there any example of creating charts and graphs in Java? thanks   Hi, check the tutorial: Chart & Graphs Tutorials in Java Thanks
how to create array in r
how to create array in r  Hi, In R Programming how array is created? how to create array in r? Explain me the process of array creation using.... You can create an array with the help of array keyword. Following is an example
How to create options in HTML
How to create options in HTML  How to create options in HTML? Please guide me.   Create options in HTML Code <tr> <td width="30" valign="middle">Gender:</td> <td><select
How to create variable in TensorFlow?
How to create variable in TensorFlow?  Hi, How to create variable... variable, you have to create the TensorFlow variable to use it with TensorFlow graph. Then tf.variable() method is used to create variable in TensorFlow
how to create array in python
how to create array in python  Hi, how to create array in python? I want to create an array in Python and add some values to it. Thanks   Hi, You can create a new array with the code myArray = [] and after
how to create vector in r
how to create vector in r  Hi, What is vector in R programming and how to create vector in r? Provide me some of the example code. Thanks   Hi, Vector in R is created using the key c(). Vector contains data of only
how to create interfaces in java
how to create interfaces in java  HI, Here is my code public interface validateInfo { public void validate(String empcode, String password); } class updateInfo implements validateInfo { public void update() { //code

Ads