redirect to servlet page using anchor tag

Redirect to servlet page using href

--------------------------------------------------------------------

Normally what we do ,we redirect a page to another servlet or jsp page by using action   in form
(<form method="post" action="purchaseServlet" ></form>)

But what if we want to redirect to a servlet using the anchor tag
 e.g. (<a href="purchaseservlet">Purchase</a>)



Solution
------------------------------------------------------------------------------------------



<form action="recipentServlet"  method="post">

<input type="submit" value="PrintReceipt" />
<br>
<a href="purchaseServlet">Purchase</a>

</form>




In above code snippet when user will click the submit button...by using action it will redirect to recipentServlet.java
But when user will click on Purchase link we expect it should go to the purchaseServlet.java
For that we have to add the bellow code snippet in the end of the purchaseServlet.java



protected void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
doPost(req,res);
}



purchaseServlet.java
-------------------------------------------------------------------------------------------
package com.cts.elearningmanagement.servelets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.cts.elearningmanagement.beans.Course;
import com.cts.elearningmanagement.beans.Service;
import com.cts.elearningmanagement.beans.Service_Course;
import com.cts.elearningmanagement.beans.User_Service;
import com.cts.elearningmanagement.dao.CourseModifyDAO;
import com.cts.elearningmanagement.dao.JoinCourseServiceDAO;
import com.cts.elearningmanagement.dao.PurchaseDAO;
import com.cts.elearningmanagement.dao.ServiceModifyDAO;
import com.cts.elearningmanagement.dao.UserServiceDAO;


public class PurchaseServlet extends HttpServlet {


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
your codes
.{
.
.//your codes
.}

//this is added in case redirect to servlet through the anchor tag
protected void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
doPost(req,res);
}
}
       
 #redirect to servlet page using anchor tag
# redirect to servlet page using href tag
#redirect to another servlet page using anchor thag or href or <a href>      
        

Comments

Popular posts from this blog

Git Commands With Output Log

Java Interview Preparation (conceptual)

Java 8 Function Interface