Skip to main content

Posts

Showing posts with the label Servlets

How to access EJB from a Servlet with in the same container

Since servlets represent a front-end technology you may want to contact an EJB from a servlet. With the recent push to use web applications for enterprise solutions, servlets have started to perform important roles in business applications. While servlets control the flow and validation of page presentation, they also are the main access point to the back-end business logic contained in EJBs. In our sample we are to create a login servlet with uses EJB behind to perform authentification. When contacting an EJB in the same container (the same virtual machine), you need only make use of a default instance of the InitialContext class. Our servlet contacts a Login EJB in order to process a user login. ... public class LoginServlet extends HttpServlet { // home interface for the EJB Login: private LoginHome _loginHome = null;

Servlets Interview Questions

1) What is servlet? Ans: Servlets are modules that extend request/response-oriented servers, such as java-enabled web servers. For example, a servlet might be responsible for taking data in an HTML order-entry form and applying the business logic used to update a company’s order database. 2) What are the classes and interfaces for servlets? Ans: There are two packages in servlets and they are javax.servlet and javax.servlet.http. Javax.servlet contains: Interfaces Classes Servlet Generic Servlet ServletRequest ServletInputStream ServletResponse ServletOutputStream ServletConfig ServletException ServletContext UnavailableException SingleThreadModel Javax.servlet.http contains: Interfaces Classes HttpServletRequest Cookie HttpServletResponse HttpServlet HttpSession HttpSessionBindingEvent HttpSessionContext HttpUtils HttpSeesionBindingListener 3) What is the difference between an applet and a servlet? Ans: a) Servlets are to servers what applets are to browsers. b)...

What is the difference between RequestDispatcher's forward method and HttpServletResponse's sendRedirect method?

RequestDispatcher's forward(ServletRequest request, ServletResponse response)   The forward() method of RequestDispatcher will forward the ServletRequest and ServletResponse that it is passed to the path that was specified in getRequestDispatcher(String path) . The response will not be sent back to the client and the web container (For example, Tomcat) internally redirects the request to the other JSP/Servlet. Remember , you can redirect only to a page within current servlet context. The client will not know about this change of resource on the server. The forward works better when one resource(JSP/Servlet) must perform business logic and share the results with another resource(JSP/Servlet). Because the request and response are forwarded to another resource all request parameters are maintained and available for use. Forward request, happened on server side, is transparent to the client (browser). Therefor, no history of it will be stored on the client, so using the back ...

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...

Difference between Dispatch Action and Lookup Dispatch Action

DispatchAction An abstract Action that dispatches to a public method that is named by the request parameter whose name is specified by the parameter property of the corresponding ActionMapping. This Action is useful for developers who prefer to combine many similar actions into a single Action class, in order to simplify their application design. NOTE - All of the other mapping characteristics of this action must be shared by the various handlers. This places some constraints over what types of handlers may reasonably be packaged into the same DispatchAction subclass. MappingDispatchAction: An abstract Action that dispatches to a public method that is named by the parameter attribute of the corresponding ActionMapping. This is useful for developers who prefer to combine many related actions into a single Action class. NOTE - Unlike DispatchAction, mapping characteristics may differ between the various handlers, so you can combine actions in the same class that, for exam...

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 to make http to https request where I am not using application server?

HTTP stands for HyperText Transport Protocol, which is just a fancy way of saying it's a protocol (a language, in a manner of speaking) for information to be passed back and forth between web servers and clients. You really don't need to know what it all stands for; the important thing is the letter S which makes the difference between HTTP and HTTPS. The S (big surprise) stands for "Secure". You probably didn't need me to tell you that, because you already knew it had something to do with security. If you visit a website or webpage, and look at the address in the web browser, it will likely begin with the following: http://. This means that the website is talking to your browser using the regular 'unsecure' language. In other words, it is possible for someone to "eavesdrop" on your computer's conversation with the website. If you fill out a form on the website, someone might see the information you send to that site. But if the web addres...