Skip to main content

Posts

Showing posts with the label JSP Interview Questions

JSP and JDBC Interview Questions & Answers

What is the query used to display all tables names in SQL Server (Query analyzer)? select * from information_schema.tables How many types of JDBC Drivers are present and what are they? - There are 4 types of JDBC Drivers JDBC-ODBC Bridge Driver Native API Partly Java Driver Network protocol Driver JDBC Net pure Java Driver Can we implement an interface in a JSP? - No What is the difference between ServletContext and PageContext? - ServletContext: Gives the information about the container. PageContext: Gives the information about the Request.

What's the Difference between Forward and Include?

The <jsp:forward> action enables you to forward the request to a static HTML file, a servlet, or another JSP. <jsp:forward page="url" /> The JSP that contains the <jsp:forward> action stops processing, clears its buffer, and forwards the request to the target resource. Note that the calling JSP should not write anything to the response prior to the <jsp:forward> action. You can also pass additional parameters to the target resource using the <jsp:param> tag. <jsp:forward page="test.htm" > <jsp:param name="name1" value="value1" /> <jsp:param name="name2" value="value2" /> </jsp:forward>

Bean with Indexed Properties and Accessing Indexed values through JSP Bean tags

In this example, A component is built that can perform statistical calculations on a series of numbers. The numbers themselves are stored in a single, indexed property. Other properties of Bean hold the value of statistical calculations, like the average or the sum. JSP Bean tags deal exclusively with scalar properties, the only way to interact with indexed properties such as these is through JSP scriptlets in the body of the tag to pass an array of integers to the Bean’s numbers property. "stat"  class = "DemoStatBean" > <%    double []  mynums =  { 100 , 200 , 300 , 400 , 500 )    stat..setNumbers ( mynums ) ; %> The average of 

JSP Interview Questions

107) What is JSP? Ans: JSP is a dynamic scripting capability for web pages that allows Java as well as a few special tags to be embedded into a web file (HTML/XML, etc). The suffix traditionally ends with .jsp to indicate to the web server that the file is a JSP files. JSP is a server side technology - you can’t do any client side validation with it.The advantages are: a) The JSP assists in making the HTML more functional. Servlets on the other hand allow outputting of HTML but it is a tedious process. b) It is easy to make a change and then let the JSP capability of the web server you are using deal with compiling it into a servlet and running it. 108) What are JSP scripting elements? Ans: JSP scripting elements lets to insert Java code into the servlet that will be generated from the current JSP page. There are three forms: a) Expressions of the form <%= expression %>that are evaluated and inserted into the output, b) Scriptlets of the form <% code %>that are ins...

What is the difference between request.getParameter() and request.getAttribute()?

Always do a request.getParameter() to extract request parameters (i.e. data sent by posting a html form ). The request.getParameter() always returns String value and the data come from client. Always use request.getAttribute() to get an object added to the request scope on the server side i.e. using request.setAttribute(). You can add any type of object you like here, Strings, Custom objects, in fact any object. You add the attribute to the request and forward the request to another resource, the client does not know about this. So all the code handling this would typically be in JSP/servlets. You can use request.setAttribute() to add extra-information and forward/redirect the current request to another resource.

How to add BASIC Authentication into HttpURLConnection?

Here is one sample. ... try { //Create connection url = new URL(targetURL); connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); ... BASE64Encoder enc = new sun.misc.BASE64Encoder(); String userpassword = username + ":" + password; String encodedAuthorization = enc.encode( userpassword.getBytes() ); connection.setRequestProperty("Authorization", "Basic "+  encodedAuthorization); ... //Send post data ... } catch (Exception e) { ... } finally { if(connection != null) { connection.disconnect(); } } } ...

How to use HttpURLConnection POST data to web server?

public static String excutePost(String targetURL, String urlParameters)   {     URL url;     HttpURLConnection connection = null;      try {       //Create connection       url = new URL(targetURL);       connection = (HttpURLConnection)url.openConnection();       connection.setRequestMethod("POST");       connection.setRequestProperty("Content-Type",            "application/x-www-form-urlencoded");       connection.setRequestProperty("Content-Length", "" +                Integer.toString(urlParameters.getBytes().length));       connection.setRequestProperty("Content-Language", "en-US");        connection...

Java URL Connection Interview Questions

Explain the difference between a URL instance and a URL connection instance. URL instance represents the location of a resource, and a URLConnection instance represents a link for accessing or communicating with the resource at the location Explain how to make a connection to a URL. Connection to the remote object represented by the URL is only initiated when the connect() method of the URLConnection is called. Doing this initializes a communication link between your Java program and the URL over the network. The following code show how a connection to a URL is made. try { URL xyz = new URL( http://www.xyz.com/ ); URLConnection xyzConnection = xyz.openConnection(); xyzConnection.connect(); } catch (MalformedURLException e) { // new URL() failed . . . } catch (IOException e) { // openConnection() failed . . . } Explain how to read from a remote file when we have its URL. With the help of the following code you could dir...

How do you pass parameters to a servlet from JSP

How do you pass parameters to a servlet from JSP? What kind of scope the JSP should have when we use forwardand backward button of the browser are used. 1) When you call a Servlet from a JSP by default JSP passes request and response objects to Servlet implicitly. So you canget all parameters in Servlet by usingrequest.getParameter("text1"); Why "String" is immutable in Java? 2) String is a immutable means youcan't change the value of a String object once it is intialized. Sincewhen you create a String it creates a String Object in Heap or Stringpool and assigns the reference of this object to a String variable. String str = new String("Java"); now Java is a object that iscreated in String pool. Once if you assign some String object to this"str" reference it is lost. When you are creating or adding some string value this String for eg. str =str.concat("Forum"); Here you are creating another ...