


































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



































Unit: Introduction to Java EE Programming and JSP Part A: Java EE and JSP Architecture
Messaging: JMS for asynchronous communication between applications. Security: authentication and authorization with JAAS , role based access. Dependency injection: CDI and annotations reduce boilerplate and XML. Scalability and high availability: clustering and load balancing support. Portability: a standard, vendor neutral platform. The app moves between servers. Database connectivity: JDBC and connection pooling. Importance of Java EE Faster development: the server supplies common services, so less code is written. Reliable for big systems: banking, insurance, e-commerce and government systems use it. Scalable: supports thousands of users at the same time through clustering. Secure: built-in authentication, authorization and encryption support. Standardised: one specification, many vendors. No vendor lock-in. Easy maintenance: separate tiers can be changed without breaking the others. Strong ecosystem: Spring and Hibernate build on the same ideas, and the skill is in demand for jobs.
Explanation of the steps Request: the browser sends an HTTP request for a .jsp page. The web server passes it to the JSP container (engine). Translation: the translator parses the page and converts it into Servlet source code. HTML becomes out.write() statements and Java code is copied as it is. Compilation: the compiler turns that source into a .class file (byte code). Loading and initialization: the class loader loads the servlet class, the container creates its object and calls jspInit() once. Execution: for every request _jspService() runs. It can use implicit objects , call JavaBeans and read or write the database through JDBC. Response: the generated HTML goes back through the web server to the browser. The client never sees the Java code. Components of the architecture Component Role Client (browser) Sends the request, displays the HTML Web server Receives the HTTP request, hands JSP pages to the container JSP engine / container Translates, compiles and runs JSP (Jasper in Tomcat) Translator Converts JSP into Servlet source Compiler Converts Servlet source into a class file Servlet instance Handles each request in _jspService() JavaBeans / DB Business logic and data storage
1. Directives Directives give instructions to the container about the whole page. They are processed at translation time. Syntax: **<%@ directive attribute="value" %>
3. Actions Actions are XML-style tags that perform a task at request time, such as including a page or using a bean. **** includes another page at run time. **** passes the request to another page. **** creates or finds a JavaBean. **** and **** set or read bean properties. **** sends a parameter with include or forward. 4. Comments JSP comment: <%-- note --%> is removed on the server. The client never sees it. **HTML comment: ** is sent to the browser and shows in view source. 5. Implicit objects These 9 objects are created by the container and can be used without declaring them.
Java Code style Java code with HTML inside out.println() HTML code with Java inside tags Ease of use Harder, more code to write Easier, much less code Best suited for Business and control logic Presentation of data (the view) Role in MVC Controller View Compilation Compiled manually by the developer Translated and compiled automatically by the container Changes Recompile and redeploy, sometimes restart Edit and save, the page reloads by itself Request method doGet() , doPost() , service() _jspService() (generated) Implicit objects Not available, must be created in code Available: request, response, session, out and more Tags, EL, JSTL Not supported Supported, which reduces Java code Session handling Get it with request.getSession() session object is ready by default Mapping and access Mapped with web.xml or @WebServlet Opened directly by its file URL First request Fast, already compiled Slower once, because of translation and compilation Who can work on it Java programmers Web designers and developers together Maintenance HTML changes need Java code changes Layout changes are easy and separate from logic Servlet public class HelloServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("Hello, "
Part B: JSP Scripting Tags and Implicit Objects
out.print(visits); } }
Example 2: using jspInit() <%! String appName; public void jspInit() { appName = "Student Portal"; // runs once at start } %> Welcome to <%= appName %>
Example
Sum of 10 and 20 = <%= 10 + 20 %>
Is 15 greater than 9? <%= 15 > 9 %>
Upper case: <%= "jsp".toUpperCase() %>
Current date: <%= new java.util.Date() %>
Example 2 <% String name = request.getParameter("name"); int price = 250, qty = 3; %> Hello, <%= name %>
Total bill: Rs. <%= price * qty %>
<%-- inside an attribute --%>Five objects explained
1. request Type: javax.servlet.http.HttpServletRequest (jakarta.* in newer versions) Holds everything the client sent : form data, query string, headers, cookies. Lives for one request only. Common methods: getParameter() , getParameterValues() , getHeader() , getMethod() , setAttribute(). <% String user = request.getParameter("username"); %> Welcome, <%= user %> 2. response Type: javax.servlet.http.HttpServletResponse Represents the reply sent back to the client. Used to set content type, add cookies and headers, and redirect. Common methods: setContentType() , sendRedirect() , addCookie() , setHeader(). <% response.sendRedirect("home.jsp"); %> 3. out Type: javax.servlet.jsp.JspWriter
1. request Type: HttpServletRequest Purpose: carries all data sent by the client to the server: form fields, URL parameters, headers, cookies. Scope: request. It exists for one request only, and a new one is made for the next. <%-- login.html sends: username, password --%> <% String user = request.getParameter("username"); String method = request.getMethod(); %>
User: <%= user %> (sent by <%= method %>)
**2. response** **Type: HttpServletResponse** **Purpose:** controls the reply sent back to the browser: content type, headers, cookies, redirection, status codes. **Scope: page**. <%String user = request.getParameter("username"); if ("admin".equals(user)) { response.sendRedirect("dashboard.jsp"); } else { response.sendRedirect("login.html"); } %>
3. session Type: HttpSession Purpose: keeps data about one user over many requests, such as login status or a shopping cart. HTTP itself is stateless, so the session fills that gap. Scope: session. It lasts until timeout (default about 30 minutes) or until invalidate(). Each user gets a separate session, identified by a session ID (cookie JSESSIONID ). <%-- page1.jsp --%> <% session.setAttribute("user", "Yukta"); %> <%-- page2.jsp, a later request --%> <% String u = (String) session.getAttribute("user"); %> Welcome back, <%= u %> <%-- logout.jsp --%> <% session.invalidate(); %> 4. application Type: ServletContext