Variables in a Servlet

Variables in a Servlet

Hi,

I'm using some variables in a servlet1
(which i get from a jsp, like "String x = request.getParameter("xyz");").

the servlet response is a jsp page which after taking some more parameters gets redirected to another servlet2. I want to use same variables(i.e., the value in x) I used in servlet1 in the new one(servlet2). How should i declare inorder to use this way. I know something like session attributes, but donno how to implement it. please help . please let me know the syntax.

Thanks
View Answers

June 9, 2010 at 3:13 PM

Hi Friend,

Try the following code:

1)form.jsp:

<form name="form" method="post" action="../Servlet" >
Enter Name:<input type="text" name="textname"><input type="submit" value="submit">
</form>

2)Servlet.java:

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Servlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
PrintWriter out = response.getWriter();
String name=request.getParameter("textname");
response.sendRedirect("./modified/action.jsp?name="+name);
}
}

3)action.jsp:

<form name="form" method="post" action="../ServletExample" >
<table>
<tr><td>Enter Address:</td><td><input type="text" name="textaddress"></td></tr>
<tr><td>Enter Email:</td><td><input type="text" name="textemail"></td></tr>
<tr><td>Enter Contact No:</td><td><input type="text" name="textcontact"></td></tr>
<tr><td><input type="submit" value="submit"></td></tr>
</table>
<input type="hidden" name="textname" value="<%=request.getParameter("name")%>">
</form>

4)ServletExample.java:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServletExample extends HttpServlet{
public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException{
PrintWriter out=res.getWriter();
String name=req.getParameter("textname");
String address=req.getParameter("textaddress");
String email=req.getParameter("textemail");
String contact=req.getParameter("textcontact");
out.println(name+" "+address+" "+email+" "+contact);
}
}

Hope that it will be helpful for you.
Thanks









Related Tutorials/Questions & Answers:
Variables in a Servlet - JSP-Servlet
Variables in a Servlet   Hi, I'm using some variables...");"). the servlet response is a jsp page which after taking some more parameters gets redirected to another servlet2. I want to use same variables(i.e., the value
pasing variables in jsp file - JSP-Servlet
pasing variables in jsp file  How can more than one pages of jsp files share variables and how can one pass local variables from one jsp file to another jsp file?  Hi Friend, To pass variables from JSP file
Advertisements
how to get javascript variables into jsp - JSP-Servlet
how to get javascript variables into jsp  How to pass variables from java script into jsp page  Hi Friend, Try it: 1)pass.jsp: var msg="Hello World" window.location.href="show.jsp?value=" + msg
How to implement session variables - JSP-Servlet
How to implement session variables  Hi, I have a servlet which gets parameter from a jsp page(x). it is stored in a variable in servlet as shown below. the servlet response is another jsp page(y). I should put the value of amp
Variables
Variables  What are the difference between Static variables, instance variables and local variables
Passing java variables from JSP to Servlet - return null values
Passing java variables from JSP to Servlet - return null values  I want to pass some variables from a JSP page to a servlet. These variables are from... to get java variables from JSP tp servlet? If there is some error in my code
assignment of variables - JSP-Servlet
Assignment of variables - JSP-Servlet
getting variables
getting variables   how to get variables from servlet program to webservice program
Java assignment of variables - JSP-Servlet
JSP - Update displayed content & session variables when user clicks on a button - JSP-Servlet
JSP - Update displayed content & session variables when user clicks on a button  Hi, I'm trying to setup a form in which the user can click on certain buttons (say an img of a "+" sign). When the user does that, I would like
JSP - Update displayed content & session variables when user clicks on a button - JSP-Servlet
JSP - Update displayed content & session variables when user clicks on a button  Hi, I'm trying to setup a form in which the user can click on certain buttons (say an img of a "+" sign). When the user does that, I would like
Environment variables
Environment variables  How to set Environment Variables in JAVA 6 n Tomcat 6 CLASSPATH, JAVA_HOME, PATH, & CATALINA variables plzzz plzz help me
Java - Declaring variables in for loops
Java - Declaring variables in for loops  Java - Declaring variables in for loops
instance variables - Java Beginners
instance variables  instance variables
Variables In Java
This tutorial demonstrates you about the variables and their types in java
transient variables in java
transient variables in java  hello, What are transient variables in java?   hii,ADS_TO_REPLACE_1 Transient variables are variable that cannot be serialized
Java Substituting Variables
Java Substituting Variables  Java Substituting Variables
Variables in Java
Variables       Data... variables)ADS_TO_REPLACE_1 Java Primitive Data Types Data Type... variables. A variable refers to the memory location that holds values like: numbers
Static & Instance variables in java
Static & Instance variables in java  What are the Difference between Static & Instance variables in java
servlet
servlet  How do we define an application level scope for servlet?   Application scope uses a single namespace, which means all your pages... thread safety). Application scope variables are typically created and populated
What are local variables?
What are local variables?   hi, What are local variables? thanks... a block of syntax like methods. Local variables should be initialised before accessing them. For details related to Local Variables
What are instance variables?
What are instance variables?   Hi, What are instance variables... for these non-static variables so we can say that these are related to objects (instances of the class).Hence these variables are also known as instance variables
What is JavaScript Variables ?
What is JavaScript Variables ?  Hi, My question is that What is Variable in JavaScript Programming Language. How to declare variables in JavaScript Program. Thanks, (adsbygoogle = window.adsbygoogle || []).push
Servlet
Servlet  What is Servlet
Variables in Smarty
Variables in Smarty Templates Engine for PHP Smarty Template variables... file variablesADS_TO_REPLACE_2 Smarty Variables can be declared as: {$new..._TO_REPLACE_13 You can use smarty associative array variables as well as indexed arrays
session variables server side
session variables server side  how to hold session variable for server side..?   request.getSession().setAttribute("wellSelection", uid
Servlet
Servlet  how to navigate one servlet page to another servlet page
JavaScript Variables
JavaScript Variables: Variables are like containers, which contains a value. In JavaScript variables dependent on the value, not on the datatype that means variables are of  dynamic typing. Variable names are case-sensitive
interface variables - Java Beginners
interface variables  why interface variables are final? explain me with good program example?? i knw why the variable is static but,i dont knw why it is final by default? thanks in advance
java protected variables
java protected variables   Can we inherit the Java Protected variable..?   of course you can. But I think what you mean is "override"? Is tha so? There are some restriction
Java Variables - Java Beginners
Java Variables  Hi, I had a small doubt. What are Object Reference variables in java..What actually they do.. And What actually an Object... reference variables, and how can i see what a reference variable contains.. shape
Java Variables - Java Beginners
Java Variables  Hi, I had a small doubt. What are Object Reference variables in java..What actually they do.. And What actually an Object... reference variables, and how can i see what a reference variable contains.. shape
servlet
servlet  is there any way to include pdf's in servlet
servlet
servlet  How many times the servlet is accessed
servlet
servlet  what are the methods and interfaces in the servlet api ?   Servlet Tutorials
Why are my variables not dividing properly?
Why are my variables not dividing properly?  Why are my variables not dividing properly
javascript where to declare variables
javascript where to declare variables  javascript where to declare variables   Javascript variable always be declared inside script tag... variables in Javascript   JavaScript where to declare variables To declare
servlet
servlet  what are the all necessary configuration to run a servlet
servlet
servlet  how to interact with a servlet from a swing program
Kotlin Variables
Kotlin Variables Example Learn to create and use variable to store values... variables in Kotlin.ADS_TO_REPLACE_1 Here are two ways of declare variable... we are declaring variables using two type of keywords: var - its variable
servlet
servlet  I designed 1 html form & a servlet but when I click on form I don't get output of servlet Please help
servlet
servlet  I designed 1 html form & a servlet but when I click on form I don't get output of servlet Please help
servlet
servlet  i want to create a login page with servlet using database mysql? only in servlet not in jsp plzz help me out
Variables in Java
Variable in Java In this section you will learn about variables in java. First.... There are some rules you have to follow while declaring a variables, they are as follows... of variable in java, they are as follows :ADS_TO_REPLACE_2 Instance Variables
Servlet
override Service method in the servlet when you extend GenericServlet to create servlet as it is mandatory to override it. But, when you extend HttpServlet to create a servlet then you can't override service method as there is a need to override
how to initialise variables in jsp?
how to initialise variables in jsp?   1. what is the problem in the folloing jsp <%@ page import="net.viralpatel.struts.helloworld.form.LoginForm" %> <%@ page language="java" contentType="text/html
ModuleNotFoundError: No module named 'mdx-variables'
ModuleNotFoundError: No module named 'mdx-variables'  Hi, My... 'mdx-variables' How to remove the ModuleNotFoundError: No module named 'mdx-variables' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'pytest-variables'
ModuleNotFoundError: No module named 'pytest-variables'  Hi, My... named 'pytest-variables' How to remove the ModuleNotFoundError: No module named 'pytest-variables' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'static_variables'
ModuleNotFoundError: No module named 'static_variables'  Hi, My... named 'static_variables' How to remove the ModuleNotFoundError: No module named 'static_variables' error? Thanks   Hi, In your

Ads