# Use of response.sendRedirect()
Use of response.sendRedirect() is method of HttpServletResponse. It interface can be used to redirect to another resource, It may, jsp or html file. It's is most useful for you.
Use of response.sendRedirect() is method of HttpServletResponse. It interface can be used to redirect to another resource, It may, jsp or html file. It's is most useful for you.
Example:-
package com;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
*
* @author pradeep
*/
public class LoginServlet extends HttpServlet
{
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String id=request.getParameter("t1");
String name=request.getParameter("t2");
Class.forName("com.mysql.jdbc.Driver");
//make connection
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/databaseName","root","root");
//create or udate database statement
PreparedStatement stmt=con.prepareStatement("select * from admin where id=? and name=?");
stmt.setString(1, id);
stmt.setString(2, name);
ResultSet rs=stmt.executeQuery();
//stmt.execute("insert into student values('1','a')");
{
out.println("<font color='green'>Welcome ADMIN</font>");
out.println("bb");
//response.sendRedirect("index.html");
}
else
out.println("<font color='red'>error</font>");
}
catch(Exception e){}
}
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo()
{
return "Short description";
}// </editor-fold>
}
No comments:
Post a Comment