Categories
discuss

Accessing post variables using Java Servlets

What is the Java equivalent of PHP’s $_POST? After searching the web for an hour, I’m still nowhere closer.

Answer

Your HttpServletRequest object has a getParameter(String paramName) method that can be used to get parameter values. http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletRequest.html#getParameter(java.lang.String)

Categories
discuss

How to set up a CSS switcher

I’m working on a website that will switch to a new style on a set date. The site’s built-in semantic HTML and CSS, so the change should just require a CSS reference change. I’m working with a designer who will need to be able to see how it’s looking, as well as a client who will need to be able to review content updates in the current look as well as design progress on the new look.

I’m planning to use a magic querystring value and/or a javascript link in the footer which writes out a cookie to select the new CSS page. We’re working in ASP.NET 3.5. Any recommendations?

I should mention that we’re using IE Conditional Comments for IE8, 7, and 6 support. I may create a function that does a replacement:

<link href="Style/<% GetCssRoot() %>.css" rel="stylesheet" type="text/css" />
<!--[if lte IE 8]>
    <link type="text/css" href="Style/<% GetCssRoot() %>-ie8.css" rel="stylesheet" />
<![endif]-->
<!--[if lte IE 7]>
    <link type="text/css" href="Style/<% GetCssRoot() %>-ie7.css" rel="stylesheet" />
<![endif]-->
<!--[if lte IE 6]>
    <link type="text/css" href="Style/<% GetCssRoot() %>-ie6.css" rel="stylesheet" />
<![endif]-->

Answer

In Asp.net 3.5, you should be able to set up the Link tag in the header as a server tag. Then in the codebehind you can set the href property for the link element, based on a cookie value, querystring, date, etc.

In your aspx file:

<head>
  <link id="linkStyles" rel="stylesheet" type="text/css" runat="server" />
</head>

And in the Code behind:

protected void Page_Load(object sender, EventArgs e) {
  string stylesheetAddress = // logic to determine stylesheet
  linkStyles.Href = stylesheetAddress;
}
Categories
discuss

Is this really widening vs autoboxing?

I saw this in an answer to another question, in reference to shortcomings of the Java spec:

There are more shortcomings and this is a subtle topic. Check this out:

public class methodOverloading{
     public static void hello(Integer x){
          System.out.println("Integer");
     }

     public static void hello(long x){
          System.out.println("long");
     }

     public static void main(String[] args){
         int i = 5;
         hello(i);
     }
}

Here “long” would be printed (haven’t checked it myself), because the compiler chooses widening over auto-boxing. Be careful when using auto-boxing or don’t use it at all!

Are we sure that this is actually an example of widening instead of autoboxing, or is it something else entirely?

On my initial scanning, I would agree with the statement that the output would be “long” on the basis of i being declared as a primitive and not an object. However, if you changed

hello(long x)

to

hello(Long x)

the output would print “Integer”

What’s really going on here? I know nothing about the compilers/bytecode interpreters for java…

Answer

In the first case, you have a widening conversion happening. This can be see when runinng the “javap” utility program (included w/ the JDK), on the compiled class:

public static void main(java.lang.String[]);
  Code:
   0:   iconst_ 5
   1:   istore_ 1
   2:   iload_ 1
   3:   i2l
   4:   invokestatic    #6; //Method hello:(J)V
   7:   return

}

Clearly, you see the I2L, which is the mnemonic for the widening Integer-To-Long bytecode instruction. See reference here.

And in the other case, replacing the “long x” with the object “Long x” signature, you’ll have this code in the main method:

public static void main(java.lang.String[]);
  Code:
   0:   iconst_ 5
   1:   istore_ 1
   2:   iload_ 1
   3:   invokestatic    #6; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
   6:   invokestatic    #7; //Method hello:(Ljava/lang/Integer;)V
   9:   return

}

So you see the compiler has created the instruction Integer.valueOf(int), to box the primitive inside the wrapper.

Categories
discuss

Why doesn’t Java autoboxing extend to method invocations of methods of the autoboxed types?

I want to convert a primitive to a string, and I tried:

myInt.toString();

This fails with the error:

int cannot be dereferenced

Now, I get that primitives are not reference types (ie, not an Object) and so cannot have methods. However, Java 5 introduced autoboxing and unboxing (a la C#… which I never liked in C#, but that’s beside the point). So with autoboxing, I would expect the above to convert myInt to an Integer and then call toString() on that.

Furthermore, I believe C# allows such a call, unless I remember incorrectly. Is this just an unfortunate shortcoming of Java’s autoboxing/unboxing specification, or is there a good reason for this?

Answer

Java autoboxing/unboxing doesn’t go to the extent to allow you to dereference a primitive, so your compiler prevents it. Your compiler still knows myInt as a primitive. There’s a paper about this issue at jcp.org.

Autoboxing is mainly useful during assignment or parameter passing — allowing you to pass a primitive as an object (or vice versa), or assign a primitive to an object (or vice versa).

So unfortunately, you would have to do it like this: (kudos Patrick, I switched to your way)

Integer.toString(myInt);
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..