Hi Friends

Hi friends,

I am new to this servlet environment. i am facing this problem, when i am running the below code.

means, when i submit username and password in the text box it is ok.

but when i should not enter any values in username and password,

i am getting the output.not the form.html page

form.html

-

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>form.html</title>

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="this is my page">

<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">

<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

</head>

<body>

<center>

<form name="myform" method="get" action="first">

<table>

<tr>

<td>Username:</td>

<td><input type="text" name="username" size="35"></td>

</tr>

<tr>

<td>Password

</td>

<td><input type="password" name="password" size="35"></td></tr>

</table>

<input type="submit" name="submit" value="send">

</form>

</center>

</body>

</html>

Example1.java

package abcd;

import java.io.IOException;

import javax.servlet.RequestDispatcher;

import javax.servlet.ServletContext;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class Example1 extends HttpServlet {

public void doGet(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException

{

doPost(request,response);

}

public void doPost(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException

{

//retriving client sent parameters

String username=request.getParameter("username");

String password=request.getParameter("password");

if(username!=null&& password!=null)

{

response.sendRedirect("form.html");

return;

}

else

{

request.setAttribute("username", username);

ServletContext context=getServletContext();

RequestDispatcher rd=context.getRequestDispatcher("/second");

rd.forward(request, response);

return;

}

}

}

Example2.java

package abcd;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class Example2 extends HttpServlet {

public void doGet(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException

{

doPost(request,response);

}

public void doPost(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException

{response.setContentType("text/html");

PrintWriter out=response.getWriter();

String username=(String)request.getAttribute("username");

if (username!=null)

{

out.println("usernameis"+username);

return;

}

else

{

response.sendRedirect("form.html");

}

}

}

hi friends ,

advance thanks for ur reply

[3757 byte] By [czvca] at [2007-11-27 5:05:36]
# 1
> but when i should not enter any values in username> and password,> i am getting the output.not the form.html pageWhat exactly do you want to achieve? If an user does not enter the name or the password, then it should redirect the user to form.html?
BalusCa at 2007-7-12 10:24:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
yes,i required to redirect to form.html
czvca at 2007-7-12 10:24:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

if (username != null && password != null) {

response.sendRedirect("form.html");

return;

}

Why have you coded that it should redirect to form.html if the username and the password are not null?

BalusCa at 2007-7-12 10:24:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

hi friend,

just now i changed Example1.java.

please observed.

my requirementis:

if i should not enter any username or password ,

second else statement is going to execute.

i mean redirect to form.html.

but i didnot get .till it is going to forward to Example2.java

Example1.java

package abcd;

import java.io.IOException;

import javax.servlet.RequestDispatcher;

import javax.servlet.ServletContext;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class Example1 extends HttpServlet {

public void doGet(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException

{

doPost(request,response);

}

public void doPost(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException

{

//retriving client sent parameters

String username=request.getParameter("username");

String password=request.getParameter("password");

if(username!=null&& password!=null)

{

request.setAttribute("username", username);

ServletContext context=getServletContext();

RequestDispatcher rd=context.getRequestDispatcher("/second");

rd.forward(request, response);

return;

}

else

{

response.sendRedirect("form.html");

return;

}

}

}

czvca at 2007-7-12 10:24:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...