Showing posts with label Servlet Interview Questions. Show all posts
Showing posts with label Servlet Interview Questions. Show all posts

Sunday, March 29, 2020

Difference Betwixt Genericservlet Vs Httpservlet Inwards Servlet Jsp - J2ee Question

Difference betwixt GenericServlet together with HttpServlet is 1 of the classic Servlet Interview Question, asked on many Servlet together with JSP Interviews on ii to four years sense developers. Since both GenericServlet together with HttpServlet forms footing of Servlets its of import to know What are they together with What is principal divergence betwixt them. From mutual sense together with at that spot names, its obvious that GenericServlet is a generic together with protocol independent implementation of Servlet interface piece HttpServlet implements HTTP protocol specifics. If you lot are working inwards Java spider web application or J2EE projects, you lot are around probable to bargain amongst HttpServlet all fourth dimension every bit HTTP is principal communication protocol of web. In this Servlet JSP article nosotros volition outline around of import difference betwixt HttpServlet together with GenericServlet which is worth knowing together with remembering.

GenericServlet vs HttpServlet

Here is my listing of divergence betwixt HttpServlet together with GenericServlet inwards Java Servlet API :

1) GenericServlet provides abstract service(ServletRequest, ServletResponse) method to implement which gets called past times container whenever it receives asking for processing, On the other manus HttpServlet overrides service method together with provides callback on doGet(HttpServletRequest request, HttpServletResponse) together with doPost(HttpServletRequest request, HttpServletResponse response) whenever it receives HTTP asking from GET or POST method. It too provides several other method based upon diverse HTTP methods of sending asking e.g. doPut() or doDelete() to conduct maintain HTTP PUT together with HTTP DELETE request.



2) Another difference betwixt GenericServlet together with HttpServlet is that afterward is a subclass of GenericServlet together with inherit properties of GenericServlet.

3) Generic servlet provides a rather easier agency to extend Servlet, its plenty to override service method to implement GenericServlet. Apart from extending Servlet interface, it too implements ServletConfig interface together with provides agency to stimulate got initialization parameter passed to Servlet from web.xml e.g. past times using getInitParamter().

 forms footing of Servlets its of import to know What are they together with What is principal divergence b Difference betwixt GenericServlet vs HttpServlet inwards Servlet JSP - J2EE question
That's all on difference betwixt GenericServlet together with HttpServlet inwards Servlet API. Its real rare you lot are going to run GenericServlet simply its practiced to know basics every bit its generally asked inwards diverse Servlet interviews every bit well. HttpServlet is primary Servlet flat which is used inwards Java spider web application from treatment customer request.

Further Learning
Difference betwixt URL Encoding together with URL rewriting

Sunday, November 24, 2019

Can You Lot Declare Constructor Within Servlet Class?

Yes, Servlet tin conduct maintain Constructor, it's perfectly legal but it's not the correct agency to initialize your Servlet. You should purpose the init() method provided past times the Servlet interface to initialize the Servlet. If you lot remember, Servlet's are particular inward a feel that they are instantiated past times the container as well as managed past times the container. Influenza A virus subtype H5N1 servlet container similar Tomcat creates a puddle of multiple Servlets to serve multiple clients at the same time. They instantiate Servlet past times calling the default no-argument constructor as well as suppose you lot conduct maintain declared unopen to other constructor which takes a parameter e.g. HelloServlet(String name) than Java compiler volition non add together the default no-argument constructor as well as Servlet container volition non able to initialize the Servlet. That's why it's of import non to render a constructor inward Servlet, but if you lot do, brand certain you lot besides add together a default constructor in that place for Servlet container.


Even though Servlet is the backbone of Java Web Application, many Programmer doesn't know commutation of Servlet e.g. who creates Servlet instance, life-cycle of Servlet instance, thread-safety as well as concurrency, Session administration etc. If you lot are only learning Servlet as well as JSP inward hotch potch manner, perhaps this is the fourth dimension to alternative upwardly a practiced mass similar Head First Servlet as well as JSP as well as read it from start to end. This is an former mass which is non yet updated to comprehend Servlet 3.0 as well as Asynchronous Servlet but all the same a practiced mass to larn fundamentals. Once yo acquire through this book, you lot tin response questions similar this past times yourself.

s are particular inward a feel that they are instantiated past times the container as well as managed past times the co Can You Declare Constructor within Servlet Class?



java.lang.InstantiationException inward Servlet

Sometimes, when developer declares constructor on Servlet they add together a parameterized constructor i.e. a Servlet constructor which accepts a parameter. Since coffee alone adds the default constructor inward whatever shape if in that place is no constructor specified, it volition non add together default constructor if you're Servlet shape already conduct maintain one. In this case, if Servlet container e.g. Tomcat tries to instantiate a Servlet inward guild to serve the outset asking from client, it volition throw the next exception:



HTTP Status 500 - Error instantiating servlet shape HelloServlet

javax.servlet.ServletException: Error instantiating servlet shape HelloServlet
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
java.lang.Thread.run(Thread.java:662)


Root cause:

java.lang.InstantiationException: HelloServlet
at java.lang.Class.newInstance0(Class.java:340)
at java.lang.Class.newInstance(Class.java:308)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1149)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:827)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)

You tin endeavour this yourself, only do a Servlet without a default no-argument constructor. Though it's only a error if you lot are sure, you lot tin conduct maintain your constructor but retrieve Servlet container calls that. I suggest, you lot should purpose the init() method to initialize the Servlet because that's the measure way.

Further Learning
Java Web Fundamentals By Kevin Jones
Spring Framework 5: Beginner to Guru
JSP, Servlets as well as JDBC for Beginners: Build a Database App

>

Saturday, November 23, 2019

6 Divergence Betwixt Forward() Together With Sendredirect() Inwards Servlet - Java

Servlet inwards JEE platform provides 2 methods forward() as well as sendRedirect() to road an HTTP asking to about other Servlet for processing. Though, both are used for forwarding HTTP requests for farther processing at that spot are many differences betwixt forward() as well as sendRedirect() method e.g. frontward is performed internally past times Servlet, but a redirection is a two-step process, where Servlet instruct the spider web browser (client) to become as well as fetch about other URL, which is unlike from the original. That's why forward() is every bit good known every bit a server-side redirect as well as sendRedirect() is known every bit the client-side redirect. Becuase of their usefulness, the departure betwixt forward() as well as sendRedirect is every bit good a oft asked Servlet interview question. Let's run into a twain of to a greater extent than differences to answer this query better.

While working betwixt Servlet as well as JSP, yous volition often utilisation these asking re-direction methods similar include(), forward(), or sendRedirect(). Sometimes, yous volition include the response from other Servlet as well as sometimes it volition merely frontward the asking for processing to about other Servlet.

Btw, if yous are preparing for Java JEE interviews as well as learning this every bit utilisation of your interview preparation, yous should every bit good banking concern jibe the Java Programming Interview Exposed, 1 of the rare majority which covers all of import topics for Java JEE interviews. It covers kernel Java, Spring, Hibernate, JDBC as well as other advanced topics expected from both beginner as well as experienced Java programmers.

 Servlet inwards JEE platform provides 2 methods  vi Difference betwixt forward() as well as sendRedirect() inwards Servlet - Java



Forward vs SendRedirect inwards Servlet

Now, let's run into about of import differences betwixt sendRedirect() as well as forward() method of Servlet API:



1) First as well as most of import departure betwixt the forward() as well as sendRedirect() method is that inwards instance of former, redirect happens at server destination as well as non visible to client, but inwards instance of later, redirection happens at customer destination as well as it's visible to client.

2) Another key departure betwixt forward() as well as sendRedirect() is that frontward is marginally faster than the redirect. The sendRedirect() is marginally slower than a forward, since it requires 2 browser requests, non one. See Head First Servlet as well as JSP for to a greater extent than details.

3) Third departure betwixt forward() as well as sendRedirect() method is that inwards instance of forward() master URL remains intact, piece inwards instance of sendRedirect() browser knows that it's making a novel request, as well as then master URL changes.

context (Some containers brand provisions for cross-context communication but this tends non to last real portable). But the sendRedirect() method tin give the sack last used to redirect users to resources that are non utilisation of the electrical flow context, or fifty-fifty inwards the same domain. See Head First Servlet as well as JSP for to a greater extent than details.

6) In the instance of forward, Since both resources are utilisation of the same context, the master asking context is retained. On the other hand, since post redirect involves a novel request, the previous asking orbit objects, amongst all of its parameters as well as attributes are no longer available after a redirect. (Variables volition demand to last passed past times via the session object).


That's all virtually  the difference betwixt forward() as well as sendRedirect() inwards Servlet. It's 1 of the of import concepts to larn for Java spider web developers. It's non merely of import from the interview signal of thought but every bit good from writing real-world Java spider web application using Servlet, JSP, jQuery as well as other JEE technologies. Practically, yous volition convey several choices for  redirecting HTTP asking e.g. yous tin give the sack produce as well as then past times using jQuery every bit well, but yous demand to utilisation sendRedirect() as well as forward() if yous desire to produce as well as then from Servlet.

Further Learning
Java Web Fundamentals By Kevin Jones
Spring Framework 5: Beginner to Guru
JSP, Servlets as well as JDBC for Beginners: Build a Database App

4 Ways Of Session Administration Inwards Servlet Java

Session tracking or Session management is an of import characteristic of modern spider web applications which allows the server to think it's clients. By keeping a session for each user, Server tin serve the customer better. It likewise helps inwards safety, security in addition to personalization in addition to must for sure enough sort of spider web applications e.g. e-commerce sites similar Amazon or e-bay which stores item selected past times the user for buy inwards a shopping cart, fifty-fifty after the user is logged out. Since HTTP is a stateless protocol, in that place are no ways to know that 2 HTTP requests are related to each other i.e. they are coming from the same customer or they are business office of the same process. Session tracking is a machinery that Servlets in addition to Java Web application purpose to maintain soil nearly a serial of asking from the same user across closed to menstruation of time. By keeping a session, an e-commerce site tin maintain add together to bill of fare facility in addition to likewise overstep on tracks of how y'all interact amongst the application. Since HTTP doesn't render a default agency to rails Session, in that place are closed to non-standard ways to create create Sessions inwards Servlet JSP based application. Let's convey a closed await on them.



Types of Session Tracking inwards Servlet

Since Session management needs to go amongst all spider web browsers in addition to likewise considers user's security preference, oft an identifier i.e. a SessionId is used to overstep on rails of asking coming from the same customer during a fourth dimension duration. There are 4 master copy ways to create create Session inwards Java Web application written using Servlet in addition to JSP.

1) URL rewriting
2) Cookies
3) Hidden Form fields
4) HTTPS in addition to SSL


let's meet them inwards picayune flake to a greater extent than detail:



URL rewriting
URL rewriting is a method of session tracking inwards which closed to extra information (session ID) is appended at the halt of each URL. This extra information identifies the session. The server tin associate this session identifier amongst the information it has stored nearly that session. This method is used amongst browsers that create non back upwards cookies or where the user has disabled the cookies. If y'all involve to rails Session from JSP pages, in addition to thence y'all tin use <c:out> tag for URL-rewriting. It automatically encodes session identifier inwards URL.


Hidden Form Fields
This is i of the oldest ways to create session tracking inwards Servlet application. Similar to URL rewriting. The server embeds novel hidden fields inwards every dynamically generated shape page for the client. When the customer submits the shape to the server the hidden fields position the client. See Head First Servlet in addition to JSP for to a greater extent than details on how to purpose hidden shape plain to create create session inwards Servlet JSP.

 Session tracking or Session management is an of import characteristic of modern spider web applications 4 ways of Session management inwards Servlet Java



Cookies
H5N1 cookie is a pocket-sized amount of information sent past times a servlet to a Web browser. H5N1 cookie is saved past times the browser in addition to subsequently sent dorsum to the server inwards subsequent requests. H5N1 cookie has a name, a unmarried value, expiration appointment in addition to optional attributes. H5N1 cookie's value tin uniquely position a client. Since a customer tin disable cookies, this is non the most secure in addition to fool-proof agency to create create the session. If Cookies are disabled in addition to thence y'all tin fallback to URL rewriting to encode Session id e.g. JSESSIOINID into the URL itself.

 Session tracking or Session management is an of import characteristic of modern spider web applications 4 ways of Session management inwards Servlet Java


Secure Socket Layer (SSL) Sessions
Web browsers that back upwards Secure Socket Layer communication tin purpose SSL's back upwards via HTTPS for generating a unique session key every bit business office of the encrypted conversation. Modern days online meshing banking website, ticket booking websites, e-commerce retailers similar Amazon in addition to e-bay all purpose HTTPS to security transfer information in addition to create create the session. You tin likewise see Murach's Java Servlets in addition to JSP to larn to a greater extent than nearly how HTTPS tin endure used amongst Java spider web application.


That's all nearly different ways to rails Session inwards Java Web application. Cookie was the most pop agency to create create Session amongst a fallback to URL rewriting when Cookies are non enabled at the customer side. While to a greater extent than security sensitive applications e.g. online e-commerce portals similar Amazon, FlipCart, EBay, online banking websites, go booking websites, or whatever other websites which bargain amongst sensitive information e.g. personal, fiscal or professional person they purpose SSL in addition to HTTPS to secures transfer in addition to maintain them.

Further Learning
Java Web Fundamentals By Kevin Jones
Spring Framework 5: Beginner to Guru
JSP, Servlets in addition to JDBC for Beginners: Build a Database App

Friday, March 4, 2011

Difference Betwixt The Getrequestdispatcher Together With Getnameddispatcher Inwards Servletcontext?

The ServletContext course of educational activity provides 2 methods getRequestDispatcher(String url-pattern) and getNamedDispatcher(String name), which tin plough over notice last used to dispatch a asking to a especial servlet. The primary divergence betwixt getRequestDispatcher() together with getNamedDispatcher() method is that old accepts a URL blueprint or path spell after agrees amongst a Servlet mention defined inwards deployment descriptor or web.xml file, both render an event of RequestDispatcher class, which tin plough over notice farther piece of job to forwards or redirect a asking inwards Java spider web application. Let' run into roughly to a greater extent than especial most these 2 methods together with how they work.


getRequestDispatcher(String url-pattern)

This method is defined inwards ServletContext course of educational activity together with it returns a RequestDispatcher object that acts equally a wrapper for the resrouce located at the given URL blueprint or path, for example, if nosotros guide keep next servlet mapping inwards our deployment descriptor :

<servlet-mapping>
<servlet-name>InfoServlet</servlet-name>
<url-pattern>/info</url-pattern>
</servlet-mapping>

than

RequestDispatcher dispatch = request.getRequestDispatcher("/info");

volition render RequestDispatcher for InfoServlet, hither /info represents the url-pattern of the InfoServlet.

You tin plough over notice equally good piece of job RequestDispatcher to forward a asking to a JSP page, equally shown below.

RequestDispatcher dispatch = request.getRequestDispatcher("/index.jsp");

Here path to index.jsp is relative to the origin of the spider web application, that's why nosotros guide keep a forwards slash prefix: "/index.jsp" to forwards the request, only telephone band dispatch.forward(request, response) where asking together with response are HttpServletRequest together with HttpServletRespose object.

If you lot are interested to larn to a greater extent than most asking forwarding inwards Servlet, you lot tin plough over notice farther see books)
  • Difference betwixt ServletContext and ServletConfig inwards Servlet? (answer)
  • 6 Difference betwixt forward() together with sendRediret() in Servlet? (answer)
  • Difference betwixt jsp:include together with jsp:forward activity inwards JSP? (answer)
  • 10 Servlet together with JSP Interview Questions for Java Programmers (questions)
  • Difference betwixt GenericServlet together with HttpServlet inwards Servlet API? (answer)
  • How to care customer sessions inwards a Servlet application? (answer)
  • Difference betwixt include directive together with include activity inwards JSP? (answer)
  • 5 Free Courses to larn Servlet together with JSP in-depth (courses)
  • Difference betwixt the spider web server, application server, together with Servlet container? (answer)
  • Can you lot declare Constructor within the Servlet class? (answer)
  • Thanks for reading this article thence far. If you lot similar these Servlet Interview questions, together with thence delight portion it amongst your friends together with colleagues. If you lot guide keep whatever questions or feedback, together with thence delight drib a note. 

    P. S. - If you lot are novel into Servlet together with JSP together with looking for a gratis tutorial or online course of educational activity to larn basics, I propose you lot become through Java Servlets together with JSP - Build Java EE(JEE) app inwards 25 Steps - a gratis course of educational activity on Udemy. It's completely free, all you lot necessitate to exercise is exercise an Udemy trace organization human relationship to enroll inwards this course.