Redirect from servlet to servlet

Redirect from servlet to servlet

I want to insert data from Text box to oracle database ,so in my HTML where there are text box (and 3 Buttons-New ,Update and save)so when i click any of three buttons it takes me to redirectServlet , and from this servlet I want to again forward my page to insertServlet(i.e When I click NEW Button) ,but I am getting 404 error . May be my logic is wrong or wex.xml needs to be mapped properly . Can anyone help please Its urgent.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>AcerLink</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

     <servlet>

    <display-name>redirectServlet</display-name>
    <servlet-name>redirectServlet</servlet-name>
    <servlet-class>redirectServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>redirectServlet</servlet-name>
    <url-pattern>/redirectServlet</url-pattern>
  </servlet-mapping> 

  <servlet>
    <description></description>
    <display-name>insertServlet</display-name>
    <servlet-name>insertServlet</servlet-name>
    <servlet-class>insertServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>insertServlet</servlet-name>
    <url-pattern>/insertServlet</url-pattern>
  </servlet-mapping>
</web-app>

redirectServlet

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

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

/**

 * Servlet implementation class redirectServlet

 */

public class redirectServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;


    /**

     * @see HttpServlet#HttpServlet()

     */

    public redirectServlet() {

        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("NEW") != null) {


            response.sendRedirect("/insertServlet");


            return ;



        } else if (request.getParameter("UPDATE") != null) {

            request.getRequestDispatcher("/redirectServlet").forward(request, response);




        }  else if (request.getParameter("SAVE") != null) {

            request.getRequestDispatcher("save.jsp").forward(request, response);

        }

    }

}

insertServlet

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

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

/**
 * Servlet implementation class insertServlet
 */
public class insertServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public insertServlet() {
        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
        PrintWriter out = response.getWriter();
        PrintWriter pwinsert = response.getWriter();
//      PrintWriter pwdelete = res.getWriter(); 

        Connection con = null;
        PreparedStatement ps = null;
       // ResultSet rs = null;
       // Statement st = null;

        String appl=request.getParameter("app");
        String sel=request.getParameter("second");
        String url= request.getParameter("link");

        try {
            Class.forName("oracle.jdbc.OracleDriver");
        }
        catch(ClassNotFoundException ex)    {
            System.out.println("driver not loaded");
            System.exit(0); 
        } 
        String URL = "jdbc:oracle:thin:SCOTT/tiger@computer_1:1521:orcl";
        try {
            con = DriverManager.getConnection(URL); 

            if(request.getParameter("choise")==null)    {
                ps = con.prepareStatement("INSERT into TEST_LINKS (ID,APPLICATION,ENTITY,URL) VALUES(TEST_LI.nextval,?,?,?)"); 
                ps.setString(1,appl);
                ps.setString(2,sel); 
                ps.setString(3,url); 

                int i = ps.executeUpdate(); 
                pwinsert.println(i); 

                if(i!=0)    { 
                    pwinsert.println("Your data has been stored in the database"); 
                } 
                else    { 
                    pwinsert.println("Your data could not be stored in the database"); 
                } 
            }
        } 
        catch(Exception e)  {
            pwinsert.println(e.getMessage());
        } 

    }

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

}
View Answers









Related Tutorials/Questions & Answers:
Redirect from servlet to servlet
Redirect from servlet to servlet  I want to insert data from Text box... to redirectServlet , and from this servlet I want to again forward my page to insertServlet(i.e...; <servlet-name>redirectServlet</servlet-name> <servlet
redirect to multiple links from servlet
redirect to multiple links from servlet  hello , In my servlet page... the output in browser,i need to redirect to another link like the above line. i want redirect to multiple links simultaneously. anyone help me out
Advertisements
servlet redirect problem help needed
servlet redirect problem help needed  package p; import...=getServletContext().getRequestDispatcâ?¦ re.forward(req, resp); } } this servlet is calling by a jsp..there is no problem,in this servlet the if condition always
servlet redirect problem help needed
servlet redirect problem help needed  package p; import...=getServletContext().getRequestDispatcâ?¦ re.forward(req, resp); } } this servlet is calling by a jsp..there is no problem,in this servlet the if condition always
Calling servlet from servlet .
Calling servlet from servlet .  How to call a servlet from another servlet in Java?   You can call another servlet by using... ServletException, IOException { System.out.println("Calling another servlet by using
Servlet Response Send Redirect - JSP-Servlet
from the visit table. Can you please show me how I can pass more than one column value to the next jsp? 2. I have a servlet that saves patient data from...Servlet Response Send Redirect  Hi, Thank you for your previous
send redirect in JSP - JSP-Servlet
send redirect in JSP   How can I include a message i.e "redirected to this page because blah blah" in the send redirect page?  Hi friend, Please specify your problem the message print on the redirect to the next
calling servlet from jsp
calling servlet from jsp  how to call a servlet from jsp
validating username and password in servlet and redirect to login page with error message if not valid
validating username and password in servlet and redirect to login page with error message if not valid  hi i want to validate my login page username and password in my servlet against database and if not valid want to display
Navigate from jsp to servlet - JSP-Servlet
Navigate from jsp to servlet   Hi Friends, Sample code for Navigating a page from jsp to servlet. Thanks
how to fetch data from servlet ????
how to fetch data from servlet ????  how to fetch data from servlet
ArrayList from JSP to Servlet - JSP-Servlet
ArrayList from JSP to Servlet  Hi, I have an arraylist declared in a scriplet in a jsp page. how can i access the arraylist in a servlet which...)); %> How can I access this arraylist in a servlet. How
Servlet
Servlet  What is Servlet
servlet
servlet  how to interact with a servlet from a swing program
Call servlet from javascript - JSP-Servlet
Call servlet from javascript  Hi in my application i have jsp that designs view, javascript for validation and servlet that perform business logic... screen when the user clicks submit button i have to pass this column values from
Servlet
Servlet  how to navigate one servlet page to another servlet page
write to file from servlet - JSP-Servlet
write to file from servlet  Hi, I have a jsp file where I input data and retrive data through servlet. However; when I edit data it is not showing... and POST methods. * @param request servlet request * @param response
Getting data from servlet into javascript
Getting data from servlet into javascript  How do i get json data from my servlet on to a variable in javascript n bind the data to display onto sigma grid.Has anyone Idea how to do
servlet
servlet  can i stoar record in variable which selected from table in servlet
servlet
servlet  is there any way to include pdf's in servlet
servlet
servlet  How many times the servlet is accessed
calling servlet from JS and return response from servlet to JS
calling servlet from JS and return response from servlet to JS  hello...) which inturn calls a servlet(myServlet).servlet performs some DB related task.now i want this servlet(myServlet) to return a url(eg. pages/file.jsp
servlet
servlet  what are the methods and interfaces in the servlet api ?   Servlet Tutorials
servlet
servlet  how to read a file from different folder using filereader in servlet   Hello Friend, Please visit the following link:ADS_TO_REPLACE_1 http://www.roseindia.net/servlets/servlet-read-file.shtml Here you
Send Redirect in Servlet
Send Redirect in Servlet     ... or not. If the password entered by the user is correct then the servlet will redirect... of our servlet, then there we should use sendRedirect() method.  In send
servlet
servlet  what are the all necessary configuration to run a servlet
servlet
servlet  I designed 1 html form & a servlet but when I click on form I don't get output of servlet Please help
servlet
servlet  i want to create a login page with servlet using database mysql? only in servlet not in jsp plzz help me out
servlet
servlet  I designed 1 html form & a servlet but when I click on form I don't get output of servlet Please help
Servlet
override Service method in the servlet when you extend GenericServlet to create servlet as it is mandatory to override it. But, when you extend HttpServlet to create a servlet then you can't override service method as there is a need to override
Send Redirect in Servlet
Send Redirect in Servlet      ... servlet, then there we should use sendRedirect() method.  In send Redirect... decides whether the concerned servlet can handle the request or not.  
Servlet
Servlet  What must be implemented by all Servlets?    The Servlet Interface must be implemented by all servlets
servlet
servlet  i want a program for counting the no of times the servlet has been invoked
servlet
servlet  how to create a login form using servlet using submit,edit delete button
servlet
servlet  dear sir servlet and html not run on eclips plz help me
servlet
of the Servlet API. It contains the classes necessary for a standard, protocol-independent servlet. Every servlet must implement the Servlet interface in one
servlet
servlet file which prints out the user's inputs. I need to use the post method to pass the data from html to the java servlet and also use both doGet and doPost methods in the servlet. I think, but unfortunately I have a terrible teacher
servlet session - JSP-Servlet
servlet session  hi, im working on a real estate web site.....n im stucked....actually if a user login from any page on web site suppose hi calls login servlet fromabout us page now i want the servlet to redirect me
Servlet
Servlet  I want to know the steps to write a simple servlet program... .   Hello Friend, Follow these steps: Put servlet-api.jar inside the lib folder of apache tomcat. 1)create a servlet. import java.io.*; import
Servlet
Servlet   Why is Servlet so popular?   Because servlets are platform-independent Java classes that are compiled to platform-neutral byte code that can be loaded dynamically into and run by a Java technology-enabled Web
servlet
servlet  i want to use servlet application on my web page then what should i do. I have already webspace.   Hi Friend, Please visit the following link:ADS_TO_REPLACE_1 Servlet Tutorials Thanks
Servlet
Servlet  Can a user defined function be included in a servlet? I need information regarding servlet syntax and not JSP.   Yes, you can create user defined function in Servlets. Have a look at the following link: http
servlet
servlet  I want the full coding for uploading a file in local drive D usin jsp,java class and servlet... help me thanks in advance....   ... and servlet please and thanks in advance
Servlet
Servlet  Hi, Can any one please expalin me below topics SERVLET ENGINE 2.WHY SUPER.INIT(); Thanks alot in advance!! Regards: Akash
servlet
"); PreparedStatement p1=con.prepareStatement("select * from Logintab where Userid...); } } } this is the code for .java servlet am able to run... .instead it redirect to pms.html   Hello Friend, The sendRedirect()method
servlet
servlet   Dear Deepak, is it compulsary to write the sevice() becoz i ve seen some example which does not ve sevice()..is it tue? plz replay me with thanks praveen
servlet
servlet  plz can anyone give me the link of javax library jar file. i badly need that. thanks in advance   Please visit the following link: Download Servlet API
servlet
servlet  I want a fully readymade project on online voting system with code in java servlet and database backend as msaccess.can u plz send me as soon as possible
Calling a jsp page from Servlet
Calling a jsp page from Servlet  How can I do this? Suppose I have jsp page aaa.jsp. From aaa.jsp on form action I have made a call to a servlet xxx.java. In xxx.java I have written code to retrieve data from database through
Servlet
the same error <web-app> <servlet> <servlet-name>InsertServlet</servlet-name> <servlet-class>InsertServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>

Ads