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



Home Answers Viewqa JSP-Servlet Redirect from servlet to servlet
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Ajay Kumar
Redirect from servlet to servlet
0 Answer(s)      a year ago
Posted in : JSP-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
    }

}

Advertisement
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
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
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
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
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
Send Redirect in Servlet
Send Redirect in Servlet     ... entered by the user is correct then the servlet will redirect the request... of our servlet, then there we should use sendRedirect() method.  In send
How to redirect from a HTML page?
How to redirect from a HTML page?  Hi, Is it possible to redirect from HTML page to another page on the net? I have one page and I want to redirect.../ValidUser"); Here is an example of Send Redirect in Servlet. Thanks
calling servlet from jsp
calling servlet from jsp  how to call a servlet from jsp
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.  
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
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  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
Sendredirect In Servlet
from one resource to another. In this example we will create a simple Servlet...Sendredirect In Servlet In this section you will read about the sendredirect() method. sendredirect() method is used to redirect the control to the different
Servlet
Servlet  What is Servlet
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
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  how to navigate one servlet page to another servlet page
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  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
servlet
servlet  how to read a file from different folder using filereader in servlet   Hello Friend, Please visit the following link: http://www.roseindia.net/servlets/servlet-read-file.shtml Here you will get an useful
servlet
servlet  what are the methods and interfaces in the servlet api ?   Servlet Tutorials
servlet
servlet  what are the all necessary configuration to run a servlet
servlet
servlet  i want to create a login page with servlet using database mysql? only in servlet not in jsp plzz help me out
 

 

 

DMCA.com