• Post Reply Bookmark Topic Watch Topic
  • New Topic

Not able to access variable  RSS feed

 
sekar bala
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

I want to access a StringBuffer variable in jsp.But it is showing only empty.what could be the probelm.Source code is given below

Class
package com.classes;

public class Modify
{
public static StringBuffer test=new StringBuffer(20);
public static void main(String[] a)
{
String s1="1.6.0_24";
test.append(s1);
System.out.println(test);
}
}

jsp file

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="com.classes.Modify" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test Variables</title>
</head>
<body>
<%
String s2=Modify.test.toString();
out.println(s2);
%>
</body>
</html>

Here s2 is having empty string.How to solve this.Please help me.
 
Shashank Ag
Ranch Hand
Posts: 88
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well as you seem to very beginer, I'll advice you not to use scriplets. Its bad practice.
However, basic problem is, your "main" method sets value in string buffer 'test' which is never called from jsp.
 
sekar bala
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
test variable is called inside the scriplet.See once again.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As has already been pointed out, as a beginner you should start off with good practices from the start. Putting Java code and scriptlets in JSPs is poor practice that has been discredited for almost a decade now. Start off on the right foot and learn modern JSP techniques including the JSTL and EL.
 
Reyaz Ahmed
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

Actually you are trying to called a Static Mamber Variable "Modify .test" from JSP whihc is modify in the main method so that you are getting the blank value in JSP
Define a different static method and append the desire String into that StringBuffer test then you are able to access it into the JSP page

like this

public static StringBuffer modified(){
String str="reyazahmed";
test.append(str);
System.out.println(test);
return test;

}

and called it in jsp like
out.println(Modify.modified());
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!