Examples of JSP in resin

Source: Internet
Author: User
Tags access
JS by Scott Ferguson
Blueski compilation

The directory is as follows:
1 Introduction
2 framework of Paradigms: Hello, World
3 Servlet Review
4 Show Message Book
5 The mode of the message book
6 as application properties of the message book
7 The logic of the message book
8 Conclusion


1 Introduction

The powerful advantage of JSP is that it separates the business logic of an application from its introduction. In Smalltalk object-oriented terminology, JSP encourages MVC (Model-view-controller) Web applications. The classes or beans of the JSP is the model, the JSP is the view, and the servlet is the controller.

This example is a simple message book, including user login and message. It is used as a demonstration of the resin platform:
--performing roles
--Model A message Book
--Login.jsp for new users
--add.jsp for registered users
--Controller guestjsp, a servlet to manage the state


2 model frame: Hello, World

The framework of the guestjsp servlet passes the string "Hello, World" to the login.jsp page. This framework for the message of the establishment of the structure. Specific details will be added below.

This example is compiled to browse to:

Http://localhost:8080/servlet/jsp.GuestJsp

You can see on the page there is this display: Hello, World

The JSP template is formatted with the processing of the servlet and then passes the processing results to the JSP page.

The following uses a Servlet2.1 ServletContext feature Getrequestdispatcher ().
The requested scheduler sends Servlets directly forward on the server and includes any possible child requests. This is a more flexible alternative to SSI inclusion.
The scheduler requested in the Servlet file can contain the results of any page, servlet, or JSP. Guestjsp will use Dispatcher.forward () to format the control to the JSP page.

GuestJsp.java:Skeleton package JSP. guestjsp;

Import java.io.*;
Import java.util.*;

Import javax.servlet.*;
Import javax.servlet.http.*;

/**
* Guestjsp is a servlet controlling user
* Interaction with the guest book.
*/
public class Guestjsp extends HttpServlet {
/**
* Doget handles GET requests
*/
public void Doget (HttpServletRequest req,
HttpServletResponse Res)
Throws Servletexception, IOException
{
Save the message in the request for login.jsp
Req.setattribute ("message", "Hello, World");

Get the Application Object
ServletContext app = Getservletcontext ();

Select Login.jsp as the template
RequestDispatcher disp;
DISP = App.getrequestdispatcher ("login.jsp");

Forward the request to the template
Disp.forward (req, res);
}
}


Servlet and JSP pages communicate using properties in the HttpRequest object. Skeleton the "Hello, World" in the "message" attribute.
When login.jsp starts, it catches the string and prints it out.

Because resin JavaScript can read the extended bean model, it can convert Request.getattribute ("message") to
The JavaScript counterpart is request.attribute.message.

Login.jsp:Skeleton <%@ page Language=javascript%>

<title><%= Request.attribute.message%></title>

<body bgcolor= ' White ' >
</body>

3 Review of the servlet

For people who come from an ASP or CGI background and turn to JSP,
Servlets instead of CGI scripts embodies the advantages of Java in dynamic class loading. The servlet is a Java class,
It extends the servlet or httpservlet and places it in the appropriate path. Resin will automatically load the servlet and execute it.

Url/servlet/classname submits the request to the servlet requestor. The requestor automatically loads the class name of the Java class from the Doc/web-inf/classes
and attempts to execute the Servlet's service method.

Resin will periodically check the class file to determine if it has been modified. If modified, the old will be replaced with a new servlet.

4 Show Message Book

After the basic framework has been run, the next step is to create model.

5 Message This model

The message is very direct, here knowledge contains the API. It complies with the Bean model to simplify JavaScript.
The same APIs can work on HashMap, file-based, and database applications.

The JSP file can only access the public method. So the JSP file cannot create a new message book or add a new user.
This is the responsibility of the guestjsp servlet.

Jsp. Guest.java API package JSP;

public class Guest {
Guest ();
Public String getName ();
Public String getcomment ();
}


Resin JavaScript can read the Bean model. So JSP pages that use JavaScript can access GetName () and Getcomment ()
As attributes. For example, you can simplify the use of guest.name and guest.comment.

Jsp. Guestbook.java API package JSP;

public class Guestbook {
Guestbook ();
void Addguest (string name, string comment);
Public iterator iterator ();
}


Resin JavaScript can also read iterator () calls, so you can use JavaScript for ... Any one to obtain the user:

For (Var guest in guestbook) {
...
}



Guestbook as Application Property
To keep the example simple, guestjsp accesses guestbook in application (ServletContext). As an example,
Saving data in application is acceptable, but for fully-fledged applications it is best to use application to place data elsewhere.

Jsp. Guestjsp.java//Get the Application object
ServletContext app = Getservletcontext ();

Guestbook Guestbook;

The guestbook is stored in the application
Synchronized (APP) {
Guestbook = (Guestbook) app.getattribute ("Guest_book");

If it doesn ' t exist, create it.
if (guestbook = = null) {
Guestbook = new Guestbook ();
Guestbook.addguest ("Harry Potter", "Griffindor Rules");
Guestbook.addguest ("Draco Malfoy", "Slytherin Rules");
App.setattribute ("Guest_book", guestbook);
}
}

RequestDispatcher disp;
DISP = App.getrequestdispatcher ("login.jsp");

Synchronize the application so the JSP file
doesn ' t need to worry about threading
Synchronized (APP) {
Disp.forward (req, res);
}


The JSP file itself is simple. It gets the message book from application and displays the contents in the table. Usually
The Application object needs to be synchronized, because some clients may also be on the same page at the same time.
GUESTJSP handles synchronization carefully before the JSP file is invoked.

Login.jsp:Display Guest Book <%@ page Language=javascript%>

<title>hogwarts Guest book</title>

<body bgcolor= ' White ' >

<table>
&LT;TR&GT;&LT;TD width= ' 25% ' ><em>Name</em><td><em>Comment</em>
<%
var guestbook = Application.attribute.guest_book

For (Var guest in guestbook) {
Out.writeln ("<tr><td>" + guest.name + "<td>" + guest.comment);
}
%>
</table>

</body>


Hogwarts Guest Book
Name Comment
Harry Potter Griffindor Rules
Draco Malfoy Slytherin Rules


6 The rules of the Message (logic)--as the application properties of the message

The rules of the message book are very simple. If the user is not logged in, he will see a prompt and login table.
After logging in, he will see the prompts and add a message to the table. LOGIN.JSP gives the login page, add.jsp gives the
Add to the gossip page.

Guestjsp holds the rule information in the session variable.

Perform ' login ' to login or ' add ' to add messages. which
Name: User Name
Password: password
Comment: Message

7 message This rule ...

Name from the session
String sessionname = session.getvalue ("name");

Action from the forms
String action = request.getparameter ("action");

Name from the login.jsp form
String userName = request.getparameter ("name");

Password from the login.jsp form
String Password = request.getparameter ("password");

Comment from the add.jsp form
String comment = request.getparameter ("comment");

Login stores the user in the session
if (Action!= null && action.equals ("login") &&
UserName!= NULL &&
Password!= null && password.equals ("Quidditch")) {
Session.putvalue ("name", UserName);
}

Adds a new guest
if (Action!= null && action.equals ("add") &&
SessionName!= NULL &&
Comment!= null) {
Guestbook.addguest (sessionname, comment);
}

String template;
If not logged in, use login.jsp
if (Session.getvalue ("name") = = null)
Template = "login.jsp";
If logged in, use add.jsp
Else
Template = "add.jsp";

RequestDispatcher disp;
DISP = App.getrequestdispatcher (template);

...


Login.jsp and add.jsp only add different forms to display the code in the previous section.

login.jsp <%@ page Language=javascript%>
<title>hogwarts Guest book:login</title>
<body bgcolor= ' White ' >

<table>
&LT;TR&GT;&LT;TD width= ' 25% ' ><em>Name</em><td><em>Comment</em>
<%
var guestbook = Application.attribute.guest_book

For (Var guest in guestbook) {
Out.writeln ("<tr><td>" + guest.name + "<td>" + guest.comment);
}
%>
</table>

<form action= ' guestjsp ' method= ' post ' >
<input type=hidden name= ' action ' value= ' login ' >
<table>
<tr><td>name:<td><input name= ' Name ' >
<tr><td>password:<td><input name= ' Password ' type= ' Password ' >
<tr><td><input type=submit value= ' Login ' >
</table>
</form>
</body>


8 Conclusion

The resin sample demonstrates some of the ways to augment the message book, including adding something smart for form processing. However, since forms can only get more, even JSP templates become complicated.
There is a conclusion: xtp template.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.