Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect from servlet to servlet

Tags:

java

jsp

servlets

I want to insert data from text box to Oracle database, so in my HTML where there are text box (and three buttons: New, Update and Save) so when I click any of three buttons it takes me to redirectServlet.

From this servlet I want to again forward my page to insertServlet (ie. when I click NEW button), but I am getting 404 error. Maybe my logic is wrong or wex.xml needs to be mapped properly.

wex.xml code

<?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
    }

}
like image 455
user2948776 Avatar asked Nov 02 '13 22:11

user2948776


2 Answers

you can use either

response.sendRedirect("/insertServlet");

or

ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/insertServlet");
rd.forward(request, response);
like image 87
Hussain Akhtar Wahid 'Ghouri' Avatar answered Oct 27 '22 16:10

Hussain Akhtar Wahid 'Ghouri'


Here's the best solution I've come up with:

response.sendRedirect(request.getContextPath() + "/servlet");
like image 29
jndietz Avatar answered Oct 27 '22 15:10

jndietz