Can we use static variables or static objects in servlets ?? I want to restrict same user to enter into the application from different system when the user is already working, he needs to get a message saying "User is already in use ".
I have created static SET object in LoginServlet and inside doget() I checked if SET contains user ,if yes display him above message otherwise add the user in SET object and forward it to next page . Later while logging out user is removed from SET .
Can I follow any other approach other than using static object SET in servlets??
I use this code in Restlet Representation. I try to get the value from the Request API. But I am facing the problem as "Cannot make a static reference to the non-static method getQuery() from the type Resource".
package javaapplication3; public class JavaApplication3 { public class robot { int xlocation; int ylocation; String name; static int ccount = 0;
[Code] .....
When I'm trying to compile it with netbeans i get these errors and i just can't figure why is that!
C:UsersuserDocumentsNetBeansProjectsJavaApplication3srcjavaapplication3JavaApplication3.java:16: error: Illegal static declaration in inner class JavaApplication3.robot static int ccount = 0; modifier 'static' is only allowed in constant variable declarations
C:UsersuserDocumentsNetBeansProjectsJavaApplication3srcjavaapplication3JavaApplication3.java:30: error: non-static variable this cannot be referenced from a static context robot firstrobot = new robot(34,51,"yossi");
why using the get method(c.get(c.HOUR_OF_DAY)); gives me the correct hour(currently 19) but directly accesing c.HOUR_OF_DAY returns 11 ? It shows In the documentation that HOUR_OF_DAY is public.
import java.util.*; public class calendar2 { public static void main(String[] args) { new calendar2().timer(); } private void timer() { Calendar c=Calendar.getInstance(); //c.clear(); System.out.println(c.get(c.HOUR_OF_DAY)); System.out.println(c.HOUR_OF_DAY);
Say I have a class called ClassA which I want to hold my data. Now inside ClassA want to hold instances of a class lets call ClassB. So far we are here.
import blah.B public class A { private B myB; (Getters setters etc) public String getBString() { B.getString(); } }
However I want to have multiple extensions of ClassB which have UNIQUE static variables.
public class B-1 extends B { private static String mString; private static int mInt; }
The problem I have run into is I can't have subclasses with their own static variables. I need the A class to hold any type of B class. How can I manage this?meant
I'm new to Java and I'm trying to create a spanning tree in the desired order with 4 nodes (0,1,2,3). I've got the following code:
import java.util.ArrayList; public class A { public static boolean any(ArrayList<Integer> input) //To check for if any element in the ArrayList == 1 { boolean answer = false; for(int i=0;i<input.size();i++) { if(input.get(i)==1)
[Code] ....
What happens is that the input parameter adj and hence the original adjmat inside main gets changed everytime I enter the method "connected", and I don't want this to happen. I understand that this is due to the main method being static and hence adjmat becomes static as well, but how do I change the code such that adjmat only gets modified after coming out of the connected function, and not while inside it?
I thought static methods could never use instance variables, because they wouldn't know which instance to look at.
From Head First Java, p. 284: "A static method is not associated with a particular instance - only the class - so it cannot access any instance variable values of its class. It wouldn't know which instance's values to use."
Now I was answering some mock exam questions from Cameron McKenzie's SCJA book, and I don't understand one of the options. On page 205, the last question has an option that says: "Instance variables ... are not visible in static methods, unless passed in as arguments." This option is supposed to be correct. Now... how does that work?
So far in my assignment I have successfully opened a text file. However I am required to do more:
1) As each line of text (containing names and ages) is read a new Runner object is created with its instance variables set thus: ! (Runner class already created )!
- name : set directly set from the value in the file - agaGroup : can be worked out from the given ages: < 18 should be 'junior' > 55 should be 'senior' the rest should be 'standard'
2) the instance of Runner should be added to the list referenced by the instance variable runners.
I have used if statements to create the junior list, however I do not see the full list of names and ages in the variable runners as I am requested to.
I am sure there is a for loop involved somewhere but I do not know how to:
a) use the for loop in my method add a new Runner object with the variable mentioned.
I include the code I have done so far as a file - p.s I use Bluej.
public class MarathonAdmin { // instance variables private String runners; private String age;
When I map my servlet to the ROOT of the site, the javascript, CSS and image files are not served. The conversation between the server and browser shows the files are being sent, but they are not rendered in the browser. This happens in both Firefox and Chrome.
If I change the mapping to anything other than the root, such as /x/, everything works as it should.
so, i was reading my java book and learning about objects and methods and it starts talking about Encapsulation and mentions that it's good practice to set instance variables as private and instead of accessing the instance variables directly, we should create a set method and get method to get and set the stuff we want to pass to the class containing the object...
for example, in this class, we're passing the integer 70 for object dog one and integer 8 for object dog two for the dog class... and these these 2 integers are sent to the setsize method so we're not accessing instance variable size directly.
i dont quite get it though....if we the programmer are the one deciding what size the integer is for the dog, and the setsize method takes the one.setSize(70) or (8) and puts them in setsize(int s) as s... but only to copy that integer stored in s back to private int size.... why do we even need to bother with making these two extra methods such as setSize, getSize?
in the book it says that... well what if the code gets into the wrong hand and someone writes something like one.setSize(0) then you would get a dog with size 0 which is essentially illogical. but then again, i'm the programmer, and i am the person who writes the code and passing the right integer.The reason for public and private... that part i understand... i can see why if a variable's data can get changed amidst the code during calculations and you dont want it to directly change the original variable and have it mess up the code, but this code from the book just a bad example of demonstrating the reason? since we manually pass the information ourselves and passing it to method setSize... and all setSize does is stores it in another integer, only to copy it right away to size (which is the original private variable we were tryign to protect?
Any simple code to demonstrate how the code might end up changing an instance variable and why we would want to protect it by using private?
class GoodDog { private int size; public int getSize() { return size; } public void setSize(int s) { size = s;
How does web server differentiates between request for static web page and request for dynamic web page? i think if web server receives request for static page directly renders that to server or else if request is for dynamic web page passes that to web app which processes the request and renders that to client. bUT how does web server differentiates between both the request.
As web server has multiple threads to serve client requests in Thread Pool & to ensure Thread Safety we should not use any variables or Objects at Instance/Class level.But in case of Session Variable which one is the Best Practice as the Session object is used by all the requests to have the same Session ID.
My Code :
public class MyServlet extends HttpServlet { private static Logger log = Logger.getLogger(ClientRegistrationServlet.class); private HttpSession session; /* This is used at Instance Level*/ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
I am using a static method to convert a string to an Integer object. Next using a instance method to convert Integer object to an int.
Compiler is giving me two "cannot find symbol" errors:
One pointing to the dot operator between "Integer.valueOf(s)"
The other pointing to the dot operator between "obj.intValue()"
I have latest JDK installed: jdk-7u51-windows-x64.exe
Looks like JCL installed correctly with rt.jar file located in "lib" directory under "Program Files"
Following is source code:
Java Code:
public class StringToInt { public static void main (String args []) { String s = "125"; Integer obj = Integer.valueOf(s); int i = obj.intValue(); i += 10; System.out.println(i); } } mh_sh_highlight_all('java');
I am working on a chess game. I need to construct a game room where all the player are present and room chat is up. Also some tables where games are being played. Now my question is how to create this game room?
To me this room must need to be like static or global (if I am not mistaken) that is up when server starts and players can join this room and should be down when server is done. How can I implement such room that would stay up for infinite time.
I write a Client/Server program,trying to send and receive a object between client and server. I use the ObjectStream but there exists an EOFException on the client side when invoking readObject() .... I've tried many times but couldn't figure it out ...
server-side code
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try{ ObjectOutputStream out = new ObjectOutputStream(resp.getOutputStream()); ObjectInputStream in = new ObjectInputStream(req.getInputStream()); PersonalData pe = (PersonalData)in.readObject(); System.out.println(pe.getYearlySalary());
When does HTTP Session object is created in web application. Suppose I have a website. Home page of website is HTTP page which contains details of company and link to Login page.
Consider below mentioned user journey as scenario:
a. user arrives at home page of website b. user click on Login page c. user fill in login details on login page and click on Submit d. user is successfully authenticated and authorized from back end e. User specific page is shown f. user click on logout link g. user is successfully logged out from website h. user is redirected to home page i. user closes browser
In the above mentioned user journey,
a. at which step does HTTP session starts (means at which steps does HTTP Session object is created ? ) b. at which step does HTTP session ends ?
In case required, assume tech stack to be Java 7, Servlet 2.5, JSP, Tomcat 7, Apache web server (for static web contents).....
From what i understand static methods should be called without creating an instance of the same class . If so why would they return an instance of the same class like in the following : public static Location locateLargest(double[][] a) , the Location class being the same class where the method is defined . I don't understand this , does it mean that every field and every method in the class must be static ? Meaning that you cannot have instances of the class because everything is static . Or it's just a mistake and the class Location cannot have a static method: public static Location locateLargest(double[][] a) ?
I can't figure out what this error message "Cannot make a static reference to the non-static method getEndUserCharge(long, long, long, long) from the type UpdateUserWS" actually means.
The error is coming from:
public void updateDetailsPackage() { some unrelated code long zero=0; double endUserCharge=0; endUserCharge = UpdateUserWS.getEndUserCharge(long zero, long zero, long zero, long zero); <-------- error is here
I am trying to call an actionListener which is shown below in my PSVM :
class testMenuItemListener implements ActionListener { public void actionPerformed(ActionEvent arg0) { getContentPane().removeAll(); createPanel(); getContentPane().add(panel1); //Adding to content pane, not to Frame repaint(); printAll(getGraphics()); //Extort print all content
[Code] .....
I get the following error :
Frame.java:409: error: non-static variable this cannot be referenced from a static context menuItem1.addActionListener(new testMenuItemListener());