Servlets and JSPs Tutorial: Learn Web Applications With Java
- 10 hours on-demand video
- 2 articles
- 3 downloadable resources
- Full lifetime access
- Access on mobile and TV
- Certificate of Completion
Get your team access to Udemy's top 3,000+ courses anytime, anywhere.Try Udemy for Business
- Understand the core technologies of modern Java web programming
Know how to develop and deploy your own websites using Java
About the course, and why you should learn web programming with Java.
- Core basic Java web server technologies
- Some exciting stuff we'll cover in the course
- Web design vs. server technologies
- Travel the world with Java
- Some prerequisites
In this tutorial we'll look at what software you need to install to start developing Java-based websites. All the software you need is free; we'll be using the industry-standard Eclipse IDE together with the highly popular Tomcat server. And of course, the JDK and JRE (Java Development Kit and Java Runtime Environment).
- What software to install for Java web application development.
- Where to find the software.
- Apache HTTP server vs. Tomcat.
- Starting Tomcat using the Windows administration tool
- Starting Tomcat using Eclipse
- Dealing with the "ports in use" error message
In this tutorial we'll create a "hello world" Java servlet. A servlet is a Java class that runs in an "application server" and sends web pages back to a browser when a user somewhere in the world clicks on a URL. Here we'll get started by creating a really simple servlet that creates the simplest possible web page.
- Creating servlets
- Creating dynamic web projects in Eclipse
- The PrintWriter class
- http gets
- The init, doGet and destroy servlet methods
- The request and response objects
In this tutorial we'll look at a better way out outputting HTML, rather than having to hardcode it in your servlet. JSPs are another of the core building blocks of Java web applications, allowing you to combine HTML and Java seamlessly.
- Creating a JSP
- What are JSPs for?
- A very brief introduction to HTML
- Embedding Java in JSPs with scriptlet tags
- Displaying the date on a JSP page
- Using the "out" object in a JSP page.
In this tutorial we'll look at the web.xml file; a standard config file that enables us to, among other things, map urls to servlets and JSPs in our applications. We'll need to understand web.xml before we can move on to deploying our applications on the Internet. I'll also give you some tips that may help to ensure you will seldom have to look at web.xml, except when you want to customise your URLs.
- The web.xml file
- Some differences between Eclipse versions
- "Annotation" style deployment descriptors, and why not to use them
- Context roots, domain names and servlet mappings
- Adding a welcome page to your project
- Mapping JSPs to URLs
- What to do if your latest code doesn't seem to run in Eclipse
In this tutorial we'll look at deploying your application to a Tomcat server using a .war file. Deploying Java web applications is surprisingly simple, provided you've got a working web.xml.
- Deploying your application to a Tomcat server
- Creating .war files
- Redeploying web applications
Cloudbees.com, which I originally recommended using to upload your servlet is being or has been decommissioned. There are lots of other options around though -- for example, try https://www.openshift.com, which lets you set up lots of kinds of app server projects -- Java Tomcat included.
In this tutorial we'll look at how you can deploy your application to the Internet, and without even paying a penny.
- Google App Engine
- Clouds and their advantages
- The importance of web.xml for deployment
- Fending off the notorious "unsupported major minor version" error
- Recompiling your application with an appropriate Java version
- Dealing with the "ports already in use" error message.
We've seen so far that you can use Java in your JSPs via scriptlet tags; but to really leverage Java (as well as to keep your JSPs reasonably simple) you'll want to import your own and other Java classes into JSPs. We'll see how to do that in this tutorial.
- Where your Java code ends up in the translated servlet
- The page directive with an import attribute
- Importing Java classes into JSPs
In this tutorial we'll look at techniques for including files within other files, so that you can build your application up out of chunks of HTML contained in separate files. For instance, you can have a file containing header for all your pages which you can then include in all the pages on your website.
- The include directive
- The JSP include tag
- Static vs. dynamic includes -- which to use and when
- When you absolutely need a static include
- When you absolutely need a dynamic include
Note: I said in this tutorial that jsp:include causes a separate browser request --- but actually this isn't quite right; jsp:include causes a sort of simulated request, apparently --- not an actual request from the browser.
In this tutorial we'll look at various ways of getting a servlet or JSP page to display another page; a powerful technique that enables you to build fully-fledged Model-View-Controller applications, among many other things.
- The jsp:forward tag
- Using the request dispatcher
- The response sendRedirect method
- Forwarding vs. redirecting
The declaration tag allows you to add methods and data members to the servlet that your JSP gets translated into, which is occasionally useful .... actually its main use is probably just that it makes it clearer where your Java actually ends up when you use Java scriptlets in JSPs.
- The declaration tag
- Where your Java ends up in the translated JSP.
- Adding methods and data members to a JSP.
In this tutorial we'll look at the difference between Model 1 and Model 2 architecture; two important ways of structuring your web applications.
- Model 1 architecture - disadvantages
- Model 2 architecture: MVC (Model-View-Controller)
- What is MVC?
- Using relative URLs
- Using servlets with forwards to implement controllers
Beans are a mechanism for sharing data between JSPs and for maintaining state in your application. In this tutorial we look at a simple example of a bean, storing some information about the user.
- Creating beans
- The JSP useBean tag
- Setting bean properties
- Getting bean properties using expression tags
The notion of "scope" is very important in web programming; we'll start to look at it in this tutorial, in the context of JSP beans.
- Session scope and cookies
- Page scope
- Request scope
- Application scope
This tutorial looks at how to set bean properties from parameters; either URL parameters or, as we'll see later on, parameters set by the user submitting a form.
- Setting bean properties using the param attribute
- Using default bean behaviour to set parameters
- Using the wildcard to match bean properties to parameters
Forms are, of course, a vital part of web programming, allowing you to gather information from your users or allowing them to log in to your website. Here we'll start looking at adding forms to your applications using the ideas we've seen so far, plus some HTML.
- The "form" HTML tag
- Input controls - buttons, text fields
- The "get" method
- Retrieving submitted values
Posts are a type of HTTP request in which the data you submit doesn't appear in the URL, by-passing the data limit on URLs and making for much nicer URLs, among other things. Data from forms is usually submitted via post, not "get". We'll look at how easy it is to make your forms post data in this tutorial.
- Post vs. get
- The doPost servlet method
Beans are a handy place to put server-side validation code. If we add validation code to a bean, we can check if the information the user has entered in a form makes sense or not by filling in the bean and calling its validation method.
- Adding validation code to a "user" bean
- An example of a regular expression for validating email addresses
By bringing together some of the techniques we've seen so far, we can create a form that validates itself, allowing itself to be submitted to your servlet or confirmation page only when the user has entered valid input.
- Creating self-validating forms using beans
- The input control value attribute in HTML
- The jsp getProperty tag
- Hidden HTML input controls
In this tutorial we'll take a look at a little web application that uses a strict MVC architecture, incorporating our bean validation code and validating the bean in the controller rather than the JSP.
- An MVC example
- Validating forms in your controller servlet
- Setting request attributes
The session object allows you to store objects and data that persist for the duration of the user's session, making it an indispensable tool for maintaining application state while your user is browsing your site.
- The session object
- Retrieving the session timeout period
- Using the session object in servlets and in JSPs
- Setting and getting session attributes
- Retrieving objects from the session
How can you maintain session if the user disables cookies? In this tutorial we'll look at a form of URL rewriting that adds a session ID to your links, allowing session to be maintained.
- What happens to sessions if cookies are disabled
- Preventing errors when the session doesn't work
- Encoding (rewriting) URLs to support cookie-free sessions
- Getting the context path programmatically
- The jsessionid.
Note: some argue that session IDs in URLs are a security risk; you may decide instead either to not use sessions or to allow your site to simply not work if the user disables cookies.
If you turn of cookies in your browser while following this tutorial, don't forget to turn them on again so you can continue following the Udemy course!
If you have some parameters, for example user names and passwords, that you want to be configurable for your web application, one place to put them is web.xml. You can use the servlet context object to retrieve these parameters.
- Adding parameters to web.xml
- Using the Eclipse XML editor
- Fetching parameters using the context object
So far we've discussed scope in the context of JSPs, and we've seen various objects that exist at different scopes in servlets. Here we'll review the three servlet scoped objects that we've seen.
- The request object
- The session object
- The servlet context object
- Getting and setting object attributes
Cookies are both loved and loathed, but either way they're an essential part of the Internet. In this tutorial we'll look at how to get and set cookies in a servlet.
- The Cookie class
- Setting cookies via the response
- Getting cookies via the request
- The cookie maximum lifespan
- Setting and getting the lifespan
If you're serious about web programming, you'll definitely want to connect to a database at some point. Database connectivity can be achieved in servlets using the generic database Java API known as JDBC. In this tutorial we'll look at a simple way you can connect to a database using JDBC, that works in any kind of Java program.
- JDBC connectors
- Finding a connector jar for your database
- Connecting to a database using the DriverManager class
- Why connecting in the init() method is a terrible idea
If you're working on an industrial-strength web app, you'll definitely want to set up a JNDI data source, implementing connection pooling and connection timeouts and so preventing application crashes from using up your database connections. You'll probably also need to use JNDI if you're deploying your application to an application server that you don't personally control.
- Benefits of JNDI data sources
- Some differences between Eclipse versions
- Setting up your data source
- Where to find information on data sources for your application server
- Getting a database connection from a data source
- How to stop Eclipse caching your code
In this tutorial, as an example of doing a database query, we'll create a form that allows a user to log in to a website, only allowing the user in if the entered email address and password match those stored in the database.
- The PreparedStatement and ResultSet classes
- Wildcards in JDBC queries
- How to avoid SQL injection attack
In this tutorial we'll look at an example of sending an email using the mail API, together with an SMTP server.
- Where to find information about the mail API
- Where to find information about your mail server
- Specifying your server and authentication information
- An alternative way of getting the email session object
- Constructing and sending a message
Up till this point in the course we've covered just about everything you need to know to implement a web application; however, our code is getting messy and a lot of it is not exactly elegant. Fear not; in this tutorial we'll look at another standard technology that will allow us to greatly simply our code, making it more elegant and more maintainable.
- Introducing JSTL
- Where to find the JSTL API jar, if you need it
- The taglib directive
- The c:out tag
JSTL has an equivalent construct to the Java for loop in the form of the "foreach" loop. Here we'll take a look at foreach and some of its most useful attributes.
- The forEach loop
- The step attribute; specifying the step size
- The varStatus attribute; an easy way to test for the first or last loop iteration
You can set objects as attributes on the request, session or web context object in your servlet and references them in your JSPs using a very simple EL syntax, which we'll look at here. Basically this enables you to passing Java objects to your JSP pages and references them in JSTL.
- More on the request, session and web context objects
- Passing objects from servlet into JSPs and referencing them in EL
You can actually output stuff on your JSP page by using an EL expression directly on the page, outside of any JSTL tags. On the other hand, if you need your strings to be escaped, you need c:out. We'll take a closer look here.
- Outputting links with c:out
- Outputting links directly with EL
- The difference between direct EL and c:out
You can use the JSTL forEach loop to iterate through the items in a Java List or any one of various collection types. We'll see how to do it here, and later on this is going to come in very handy when we look at dealing with the results of SQL queries in JSTL.
- Using forEach with lists
- HTML tables -- a brief example
In this tutorial we'll look at how to take files created by a designer (or yourself) and turn them into a dynamic web project. Along the way we'll look at how to use graphics in a web application, and we'll see a really simple way to access the context root.
- Importing external files into a dynamic web application
- Using images
- A really simple way to access the context root in EL
- Picture Squirrel: an example web application
- An example of setting a servlet to the site home page
We've already seen two good ways to include content in other content; here we'll look at the JSTL way of doing it, and we'll see an example of parcelling off a header and footer into separate file.
- JSTL c:import
- Including files in other files with JSTL
- Setting parameters in JSTL imports
You can use JSTL to do SQL stuff, querying and even updating database; thus encapsulating a lot of messy SQL in the page where it's actually used and taking a huge burden of complexity off your controller servlets or other auxiliary code.
- Doing a SQL query in JSTL
- The "sql" prefix and taglib directive
It's time to take the image names we retrieved in the last tutorial and use them to actually display images. We'll also use the JSTL c:set tag to set the image names into a scoped variable which can then be used to display the images. Plus, a bit of a look at, in effect, concatenating strings in JSTL.
- The HTML image tag
- The c:set tag: creating scoped variables in JSTL
- String concatenation in JSTL
The c:url tag both prefixes your context root and also adds on the jsessionid if necessary. We'll look at it in this tutorial.
- The c:url tag
- An example of mapping a servlet to a user-friendly url
- An example of the link tag in HTML
JSTL includes some functions that you can use in EL, mostly connected to string handling. We'll take a look at a couple of examples here.
- JSTL functions
- Where to find documentation
- Getting strings from substrings
- Upper- and lower-casing strings
- Capitalising the first letter of a word in JSTL
So far we've looked at SQL queries, and in this tutorial we'll go on to looking at an example of a SQL update. We'll also take a look at transactions, which enable us to execute a bunch of SQL statement which will either all succeed or else all fail; in the case of failure, the database is left in the same state that it was prior to the transaction.
- SQL Updates in JSTL
- More EL expression examples
Congratulations on finishing the course!!! Many thanks for subscribing, and I hope you'll check out my courses in the future. In this final video I'll tell you about some other related technologies that might interest you, plus a few words about the job market.
- Thanks and congratulations!
- Client vs. server technologies
- Web frameworks
- Looking for jobs
In this video we'll look at how you can allow the user to upload files using Tomcat 6. The catch is, you either need to have a writeable directory on your server, or write some code to store uploaded files in MySQL. Assuming one of those, we'll take a look at how you can support upload forms with servlets.
NOTE: For Tomcat 7, see the second set of attached code to this lecture. Some things apparently changed in Tomcat 7, and the code I gave in this video no longer works.
- Basic Java knowledge
- Ideally basic HTML and SQL knowledge
- Basic computer and Internet skills
In this Java web application tutorial I'll show you how to create dynamic websites using the core technologies of Java web programming. If you want to create your own interactive websites, if you know some Java and you want to take your skills to the next level, or if you want hot skills for the job marketplace, this Java web application tutorial course is for you.
In this Java web application tutorial course you'll learn how to:
- Leverage the power of Java to create dynamic websites
- Deploy your applications for free on the Internet
- Use and understand core server-side Java web technologies
I'll show you how to take your basic Java knowledge and use it to create websites using the same technologies (servlets, JSPs and JSTL) that everyone from self-employed web developers to huge corporations use to create modern interactive web sites.
- Anyone who wants to create websites that interact with databases
- Java programmers who want to learn web programming skills
- Java beginners who want to learn more Java and enjoy a challenge