Giter VIP home page Giter VIP logo

javawebnotes's Introduction

Java Notes:

Servlets:

  • The name of a servlet is different from the servlet class. A servlet class can have multiple names and different urls. Imagine having a servlet class that implements the same logic for two different servlets that connect to two different databases. Basically one servlet class can map to multiple servlet names and urls.
  • To configure a project to run on a local container like tomcat, right click tomcat on Servers in the bottom in eclipse (I know this is bad) and click on add/remove.
  • Context params get defined in the deployment descriptor web.xml and one can get them with
ServletContext s = HttpServlet.getServletContext();
  • Within The deployment descriptor, we use the <context-param> as in:
<context-param>
	<param-name>database</param-name>
	<param-value>msql</param-value>
</context-param>
  • <context-param> should be listed under the web-app tag. Context params are app-wide. For specific servlets, init params are used.
  • @MultipartConfig annotation is used to control file upload. In a file descriptor, <multipart-config> tag can be put in a <servlet> tag to do the same job.

JSP:

  • JSPs combine java code, html tags, built-in jsp tags, custom jsp tags and expression language. They are used to create dynamic html pages.
  • JSPs are fancy servlets. They are compiled into java code.
  • JSPs have a variety of components including:
    1. Directives: controls how JSPs are interpreted and used as in <%@ page contentType="text/html;charset=UTF-8"%>. They are also used for importing classes and various other actions that control how a page is used.
    2. Declarations: Used to define classes, instance variables, methods.. etc. All these data are limited by the scope of the servlet generated by the JSP.
    3. Scriptlets: Similar to declarations somehow but might have a larger scope than a declaration. A variable defined in a declaration can be used in a Scriptlet but not vice-versa. you can do a lot of actions in a scriptlet like conditionals, loops.. etc.
    4. Expressions: contain java code that return something to the output (e.g. for client display).
  • The following are tags used to declare each of the four JSP components:
<%@ directive %>
<%! declaration %>
<% scriptlet %>
<%= expression %>
  • Comments take different forms in JSP's. There HTML/XML comments, java comments and JSP comments. HTML comments prevent the browser from showing the commented parts, but JSP code within such comments is still interpreted and the HTML source contains that. Ignoring this might cause security issues (I think). Within JSP tags, java code is commented as usual! JSP comments as a little similar to HMTL comments as in:
<%-- JSP comment --%>

JSP comments are remove jsp blocks all together. They are neither compiled to java nor sent to the browser.

  • Imports are done in different similarly to java code in two slightly differnt ways within directives:
<%-- You can have imports separated by commas or have different imports within separate directives -->
<%@ page .... import="java.something.Something, java. something.SomethingElse" %>
<%@ page import="java.something.Something>%
<%@ page import="java.something.SomethingElse>%
<%@ page import="java.something.YetSomethingElse>%
  • Directives are used to perform 3 tasks:
    1. Change page properties such as importing new classes, configuring sessions ..etc.
    2. Directives can also include JSPs in other JSPs. This can be done this way:
<%@ include file="/path/file.jsp" %>
This is a static include at compile time. It is fast but less dynamic. A more dynamic, albeit slower, follows this pattern:
<jsp:include page="/path/index.jsp">
3. They are also used to **include tag libraries**.
  • <jsp> Tag: JSP's support tags with the XMLNS jsp. This is an old way of doing things. An example is <jsp:include> used to include a jsp. <jsp:forward> is used to forward a page. Similar tags include <jsp:useBean>, <jsp:getProperty>, <jsp:setProperty>, <jsp:plugin> (for applet plugins).
  • JSP implicit variables are:
    1. request: You can do with it what you can do with a request in a servlet such as setting and getting parameters, attributes.. etc.
    2. response: Similar to request.
    3. out: similar to getWriter of HttpServletResponse.
    4. application: same as servletContext.
    5. config: Allows you to access configuration params and such.
    6. pageContext: used for forwarding, accessing session and request attributes and all that jazz.
    7. session: HttpSession and all that.
    8. page: Equivalent to the this variable of the JSP servlet object. Might never need to be used.
    9. exception: for errors.
  • Java in JSPs: straight java code can be used in JSPs, but it's a bad idea due the principle of separation of concerns. JSP's are intended for creating the view or presentation layer. UI developers shouldn't be bogged down by java code inside JSP's. Let them deal with the UI, while the application logic should stay in the backend improper java classes and servlets. WHAT???!!!
  • Combinging servlets and JSPs: By combining servlets and JSPs one achieves both the goal of separation of concerns and allows for easier and more straightforward development. Servlets take care of backend logic, while JSPs handle the UI. JSPs handling the UI is much easier and clearer than how sevlets sends clunky strings containing HTML.4dxsdadd

javawebnotes's People

Contributors

ahmaazouzi avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.