answersLogoWhite

JSP and Servlets

JSP (JavaServer Pages) are Java programs encoded within webpages that research and generate pertinent results. Servlets, on the other hand, are objects (programs) that handle requests from users and provide results based on these requests.

859 Questions
JSP and Servlets

What is signature of service method in servlet?

The doXXX Methods

There are 3 main types of requests that get processed by a Servlet. They are:

• Get

• Post

• Put

Each of them have a corresponding doXXX() method in the Servlet class which would be:

• doGet

• doPost

• doPut

These methods are called by the service method in your Servlet.

Let us now take a look at a sample Servlet that has these 3 doXXX methods.

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

/**

* A servlet that has the 3 doXXX Methods

*

* @author Anand

*/

public class OurSecondServlet extends HttpServlet

{

// doGet()

public void doGet(HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException

{

requestType("GET", response);

}

// doPost()

public void doPost(HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException

{

requestType("POST", response);

}

// doPut()

public void doPut(HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException

{

requestType("PUT", response);

}

public void requestType(String requestType,

HttpServletResponse response)

throws IOException, ServletException

{

response.setContentType("text/HTML");

PrintWriter out = response.getWriter();

out.println("< HTML >");

out.println("< head >");

out.println("< title >Our Second Servlet" +

"< / title >");

out.println("< / head >");

out.println("< body >");

out.println("< h1 >Servlet Request< / h1 >");

out.println("The Submitted Request type is : " + requestType);

out.println("< / body >");

out.println("< / HTML >");

}

}

212223
HTML
JavaScript
JSP and Servlets

It is a program used to view HTML documents?

A browser will view them as a web page. Other applications can be used to view the code, like any text editor or any word processor.

212223
JSP and Servlets

What is the difference between ASP and JSP?

ASP stands for "Active Server Pages"

JSP stands for "Java Server Pages"

asp(classic) is interpreted, which means the code has to be read each time a page is called. jsp is compiled into java classes which makes for a much faster page generation.

Classic asp(not the new .NET) has to rely on Active X, COM or other Microsoft technologies to really enhance functionality of the code. jsp can bring in every java capability.

Handling email in asprequires external components such as CDONTS or aspMail and was tricky to implement. JavaMail for jsp was a 10 minute download and was much easier to use.

192021
JSP and Servlets

What is servlet mapping?

A Servlet Mapping is a directive in the web.xml that tells the Servlet Container which class to use when a particular Servlet is called. A Servlet is a class within your Java Web Application. Let's say you have a servlet called MyServlet in the com.example.servlet package. You would need to have a Servlet Mapping pointing the path "/MyServlet" to the "com.example.servlet.MyServlet" class. Without the servlet mapping, you would not be able to invoke your servlet because the Servlet container would not know where it is. JSPs are different - they do not need mappings like this. JSPs exist within the WebRoot of the application, so they are always available. Servlets exist in the WEB-INF\Classes directory once your application is deployed.

212223
Downloader Viruses
Business Law
Web Downloads
JSP and Servlets

Where can you find a downloadable version of UCP600?

Thx to inform me in you could find any!! It seams to be impossible mohamed.wahdan@eg.schneider-electric.com Thx to inform me in you could find any!! It seams to be impossible mohamed.wahdan@eg.schneider-electric.com

212223
Java Programming
Database Design
JSP and Servlets

Trying to create a web application where users in different time zones will access it. How do I set up the DB and application to handle this I am using Oracle 11g and Java EE?

All access is relative to the local time zone of the server, so you simply record the local time. Knowing the server's time zone allows you determine how many hours behind or in front of UTC you are and knowing the user's time zone allows you to do the same for the user, thus you can calculate the time difference by subtracting one from the other.

For example, suppose the server is in Los Angeles, USA and the user is in Rome, Italy. Los Angeles is UTC -8 while Rome is UTC +1, so the time difference is 9 hours in total, such that Rome is 9 hours ahead of Los Angeles. So if a user accesses the server at 10:00 Los Angeles time, it would have been 19:00 in Rome.

As another example, suppose the server is in Helsinki, Finland (UTC +2), and the user is in Toronto, Canada (UTC -5). The time difference is 7 hours, such that Toronto is 7 hours behind Helsinki. So an access at 10:00 in Helsinki occurred at 03:00 in Toronto.

012
HTML
JSP and Servlets

What is the syntax to call a jsp page from HTML button?

There are several ways to use an HTML button to go to a web page. You can keep it simple. This example uses "page.jsp" in the same directory as your HTML page for your target location. You can use the following code:


012
JSP and Servlets

What is the fullform of JSP?

JavaServer Pages (JSP) is a Java technology that helps software developers serve dynamically generated web pages based on HTML, XML.

151617
JSP and Servlets

Why do you use superclass init method inside your init method in a servlet?

Because, that is how all Java classes work. When a class is initialized/created all the classes it extends from (its super classes) need to be initialized as well.

192021
Java Programming
JSP and Servlets

What is the difference between activity diagram and state diagram?

State diagram shows the object undergoing a process. It gives a clear picture of the changes in the object's state in this process.

e.g: ATM withdraw

Card object state: Checking, Approving, Rejecting

Activity diagram is a fancy flow chart which shows the flow of activity of a process.

e.g: ATM withdraw

Withdraw activity: Insert Card, Enter PIN, Check balance, with draw money, get card

State chart shows the dynamic behavior of an object.

Activity diagram shows the workflow behavior of an operation as set of actions

171819
Computer Programming
JSP and Servlets

How state Diagram are different from the Activity Diagram?

They are not as they both have the same features it just provides a better understanding when using one diagram in conjunction with the other.

State machine diagram shows how state of some object or system could change, so transitions on the diagram are from one state to another state.

Activity diagram shows set of actions and how one action could lead to another.

In simple cases these diagrams might look like reverse of each other but in practice you need to select whether the diagram is about discrete number of states of some object or is it about organization of actions and activities possibly related to many different objects.

192021
JSP and Servlets

What does servlet session API means?

The Servlet Session API refers to the documentation on the list of methods available in the HttpSession class that is used for Session Management

The HttpSession object is very powerful and has many features. Before we dig deep into the world of Sessions, let us take a look at the important methods they have so that we can use them effectively.

• getAttribute(java.lang.String name) - Returns an Object associated with that name that was stored in the session object.

• getAttributeNames() - Returns an Enumeration object that lists the names of the objects stored in the session object.

• getCreationTime() - Returns a long containing the date stamp of creation.

• getId() - Returns the id of the session as a String representing the unique identifier assigned to this session.

• getLastAccessedTime() - Returns a long containing the date stamp of the last time the session was accessed.

• getMaxInactiveInterval() - Returns an integer representing the maximum time interval, in seconds, that the servlet container will keep this session open between client accesses.

• invalidate() - This destroys a session. It can't be referenced after this method has been called.

• isNew() - This tells you whether the client knows about the session. In other words, the has session been created by the server, but the client hasn't acknowledged receiving the session ID yet.

• removeAttribute(java.lang.String name) - This removes an attribute. It deletes it from the session object.

• setAttribute(java.lang.String name, java.lang.Object value) - You use this method to add objects to a session.

• setMaxInactiveInterval(int interval) - Specifies the time, in seconds, between client requests before the servlet container will invalidate this session.

• getValue(java.lang.String name) - Returns an Object associated with that name that was stored in the session object. As of Version 2.2, this method is replaced by getAttribute(). The getAttribute() method will be on the exam, not this one, but it is here for completeness.

• getValueNames() - Returns a String array with a list of names associated with the objects added to a given session. As of Version 2.2, this method is replaced by getAttributeNames(). The getAttributeNames() method will be on the exam, not this one, but it is here for completeness.

• putValue(java.lang.String name, java.lang.Object value) - You use this method to add objects to a session. This has been deprecated. As of Version 2.2, this method is replaced by setAttribute(java.lang.String, java.lang.Object). The setAttribute() method will be on the exam, not this one, but it is here for completeness.

• removeValue(java.lang.String name) - This removes a value, but retains an attribute name in the session. The name is valid but the object is null. This has been deprecated. As of Version 2.2, this method is replaced by removeAttribute(). The removeAttribute() method will be on the exam, not this one, but it is here for completeness.

171819
Java Programming
JSP and Servlets

What function does tag serve?

A tag tells the computer to do something

171819
PHP Programming
JSP and Servlets
The Difference Between

What is the difference between sort and asort in PHP?

sort() will order the array by its values without preserving the keys. Use it when array is indexed numerically or when you do not care about the keys.

asort() will also sort the array by its values, but it will preserve the key -> value association.

171819
Computer Programming
Java Programming
JSP and Servlets
Cartoons

You are a beginner on programming with Java Server Pages could you recommend you some websites?

== ==

161718
JSP and Servlets

What is doGet method in servlet?

The doGet() method is the method inside a servlet that gets called every time a request from a jsp page is submitted. The control first reaches the doGet() method of the servlet and then the servlet decides what functionality to invoke based on the submit request. The get method called when the type of page submission is "GET"

There is another way of submitting requests from a jsp page is "POST" and when that happens it calls the doPost() method inside the servlet.

131415
Database Programming
Java Programming
JSP and Servlets

File Upload into Database in JSP?

<%@page language="java" session="true"

import="java.io.*,java.util.*,java.io.*,java.sql.*,javax.servlet.*"%>

<%

//to get the content type information from JSP Request Header

String contentType = request.getContentType();

if (contentType != null && contentType.indexOf("multipart/form-data") >= 0)

{

DataInputStream in = new DataInputStream(request.getInputStream());

//we are taking the length of Content type data

int formDataLength = request.getContentLength();

byte dataBytes[] = new byte[formDataLength];

int byteRead = 0;

int totalBytesRead = 0;

//this loop converting the uploaded file into byte code

while (totalBytesRead < formDataLength)

{

byteRead = in.read(dataBytes, totalBytesRead, formDataLength);

totalBytesRead += byteRead;

}

String file = new String(dataBytes);

//for saving the file name

String saveFile = file.substring(file.indexOf("filename="") + 10);

saveFile = saveFile.substring(0, saveFile.indexOf("\n"));

saveFile = saveFile.substring(saveFile.lastIndexOf("") + 1,saveFile.indexOf("""));

int lastIndex = contentType.lastIndexOf("=");

String boundary = contentType.substring(lastIndex + 1);

int pos;

//extracting the index of file

pos = file.indexOf("filename="");

pos = file.indexOf("\n", pos) + 1;

pos = file.indexOf("\n", pos) + 1;

pos = file.indexOf("\n", pos) + 1;

int boundaryLocation = file.indexOf(boundary, pos) - 4;

int startPos = file.substring(0, pos).getBytes().length;

int endPos = file.substring(0, boundaryLocation).getBytes().length;

// creating a new file with the same name and writing the content in new file

//FileOutputStream fileOut = new FileOutputStream(saveFile);

FileOutputStream fileOut = new FileOutputStream("C:\\Program Files\\Apache Software Foundation\\Tomcat 5.0\\webapps\\incidentreportform_main\\upload"+saveFile+"");

fileOut.write(dataBytes, startPos, (endPos - startPos));

fileOut.flush();

fileOut.close();

//out.println(saveFile);

Connection con=null,con1=null;

Statement stmt=null,stmt1=null;

PreparedStatement ps=null,ps1=null;

ResultSet rs=null,rs1=null;

String sql="",sql1="",a="";

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con=DriverManager.getConnection("jdbc:odbc:inficert","inficert","inficert");

stmt=con.createStatement();

String sno=request.getParameter("parameter");

int j1=0;

try

{

sql="UPDATE department set crftitle =('"+saveFile+"') where sno='"+sno+"'";

j1=stmt.executeUpdate(sql);

if(j1!=0)

{

%>

<script type="text/javascript">

alert('Successfully Uploaded');

top.location.href = "selectlocation.jsp";

</script>

<%

}

}

catch(Exception ex)

{

ex.printStackTrace();

}

}

%>

345
Web Programming
Software Engineering
JSP and Servlets

Why jsp cannot be used as mvc controller instead of servlets?

Because JSPs are designed to take care of the View part of the MVC design patter and not the controller. You can have little or some amount of logic in it, but it is not designed/created with the idea of controlling the flow of control in a J2EE application.

151617
JSP and Servlets

What are the advantages and disadvantages of JSP?

Advantages of JSP

1. HTML friendly simple and easy language and tags.

2. Supports Java Code.

3. Supports standard Web site development tools.

Disadvantages of JSP

1. As JSP pages are translated to servlets and compiled, it is difficult to trace

errors occurred in JSP pages.

2. JSP pages require double the disk space to hold the JSP page.

3. JSP pages require more time when accessed for the first time as they are to be

compiled on the server.

192021
JSP and Servlets

What are the servlet config methods?

Methods in the Servlet Context

Apart from setting and getting custom attributes used for our application, the context also contains various methods that we can use to retrieve specific information about the application and other aspects. They are:

• getAttributeNames() - Returns an Enumeration object containing the attribute names available within this servlet context.

• getContext(String uripath) - Returns a ServletContext object that corresponds to a specified URL on the server.

• getInitParameter(String name) - Returns a string containing the value of the named context-wide initialization parameter, or null if the parameter does not exist.

• getInitParameterNames() - Returns the names of the context's initialization parameters as an Enumeration of string objects, or an empty Enumeration if the context has no initialization parameters.

• getMajorVersion() - Returns the major version as an int of the Java Servlet API that this Servlet Container supports.

• getMimeType(java.lang.String file) - Returns the MIME type as a string of the specified file, or null if the MIME type is not known.

• getMinorVersion() - Returns the minor version as an int of the Servlet API that this Servlet Container supports.

• getNamedDispatcher(String name) Returns a RequestDispatcher object that acts as a wrapper for the named servlet.

• getRealPath(String path) - Returns a string containing the real path for a given virtual path.

• getRequestDispatcher(String path) Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path.

• getResource(String path) - Returns a URL to the resource that is mapped to a specified path.

• getResourceAsStream(String path) - Returns the resource located at the named path as an InputStream object.

• getServerInfo() Returns the name and version as a String of the Servlet Container on which the servlet is running.

151617
Microsoft Windows
Windows XP
JSP and Servlets

What does refresh do?

It renews all information (borders, panels, menues, text, data and so on) on the element where it was attached to.

91011
Web Usability
Web Programming
JSP and Servlets
IIS

What all are the Examples of application server?

Bea Weblogic, IBM WebSphere

151617
Cooking Equipment
Roman Empire
Pueblo Indians
JSP and Servlets

What makes utensils microwvave safe?

Microwave safe simply means that the material that the "utensil" or cookware is made of does not readily react to the bombardment of microwaves and many microwaves can pass through them more unobscured than with other materials. Their structures may get warm as they are excited but do not melt, spark or release their composits into food/air etc... For example metals will spark as the microwaves hit their surface, certain glasses and ceramics will get hot as their molecules build up energy from the microwaves radiating through them until they let loose and crack or explode.

151617
JSP and Servlets
How To

How-to clear the session values from the jsp page?

session.invalidate() . But you need to refresh page to take effect. Note that generally that the session truly ends only when the browser window closes.

The six most commonly used methods to invalidate a session are

• Calling HttpSession.setMaxInactiveInterval(int secs) method, explicitly setting how many minutes the session will last.

• The session will automatically be invalid after a certain time of inactivity (Tomcat default is 30 minutes). You need to remember that this 30 minutes is not a hard and fast rule for all servers. It might vary from one server to another and is configurable. So you can have it configured to last 25 mins in your server and I can have it to last 20 mins.

• The user closes all browser windows. Note that, here the session will timeout rather than directly triggering a session invalidation.

• The session will expire when it is explicitly invalidated by a servlet by calling invalidate().

• The server is stopped or crashes. Note that this event might not trigger a session invalidation. A Web container that permits failover might persist the session and allow a backup Web container to take over when the original server fails.

• You can set the default timeout in the web.xml file ().

151617

Copyright © 2020 Multiply Media, LLC. All Rights Reserved. The material on this site can not be reproduced, distributed, transmitted, cached or otherwise used, except with prior written permission of Multiply.