java4all@1986 java. Powered by Blogger.

What is meant by sendRedirect and example?

>> Friday, June 10, 2011

Send Redirect:   When we want that someone else should handle the response of our servlet,
then there we should use sendRedirect() method.
In send Redirect whenever the client makes any request it goes to the container,
there the container decides whether the concerned servlet can handle the request or not.
If not then the servlet decides that the request can be handle by other servlet or jsp.
Then the servlet calls the sendRedirect() method of the response object and sends back the response to the browser along with the status code.
Then the browser sees the status code and look for that servlet which can now handle the request.
Again the browser makes a new request, but with the name of that servlet which can now handle the request,
and the result will be displayed to you by the browser. In all this process the client is unaware of the processing.

                                  

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Redirecting the page</title>
</head>
<body>
<form action = "/ServletProject/SendRedirect" method = "post">
<tr>
<td>Enter your name :</td>
<td><input type = "text" name = "username"></td>
</tr><br>
<tr>
<td>Enter your password :</td>
<td><input type = "password" name = "password"></td>
</tr><br>
<tr>
<td><input type = "submit" name = "submit"></td>
</tr>
</form>
</body>
</html>
SendRedirct.class
import java.io.*;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SendRedirect extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
public SendRedirect() {
super();
} 
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String name = request.getParameter("username");
String password = request.getParameter("password");
if(name.equals("James")&& password.equals("abc"))
{
response.sendRedirect("/ServletProject/ValidUser");
}
else
{
pw.println("u r not a valid user");
}
} 
}
validuser.class
import java.io.*;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class for Servlet: ValidUser
*
*/
public class ValidUser extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public ValidUser() {
super();
} 

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException {
// TODO Auto-generated method stub

} 

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter pw = response.getWriter();
pw.println("Welcome to roseindia.net
"); pw.println("how are you"); } }

Read more...

Adding numbers to text box and providing output in text box?

>> Wednesday, June 8, 2011

<html>
<SCRIPT language = JavaScript>

function calculate() {
A = document.frmOne.txtFirstNumber.value
B = document.frmOne.txtSecondNumber.value
C = (A + B)
document.frmOne.txtThirdNumber.value = C
}

</SCRIPT>
<FORM NAME = frmOne>

Number One: <INPUT TYPE = Text NAME = txtFirstNumber SIZE = 5 value ="">//enter value=5;

Number Two: <INPUT TYPE = Text NAME = txtSecondNumber SIZE = 5 value ="">//enter value=6;
<P>
Total: <INPUT TYPE = Text NAME = txtThirdNumber SIZE = 5 value = "">//we will get total as 56 when we submit the button.
<P>
<Input Type = Button NAME = b1 VALUE = "Add Numbers" onClick = calculate()>

</FORM>
</html>

Read more...

How to make a page to move back page?

Use the below code to make the page to go  back
window.history.back()

Read more...

Different background colors by clicking buttpns?

<HTML>
<BODY>

<INPUT TYPE = Button VALUE = "Colour One" Onclick = "document.bgColor = 'Red'">

<INPUT TYPE = Button VALUE = "Colour Two" Onclick = "document.bgColor = 'Blue'">

<INPUT TYPE = Button VALUE = "Colour Three" Onclick = "document.bgColor = 'Green'">

</BODY>//The background colour of your web page should have changed colour when you clicked a button.
It did this because of the bgColor poperty of the document object.
</HTML>

Read more...

FaceBook Login

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

Total Pageviews

STATCOUNTER

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP