Master the document structure of web applications;

Master the operation principle of jsp;

Master the simple input and output of jsp.

main content:

Introduce the document structure and operation principle of web application through a simple example;

Introduce basic input and output through a simple registration function.

Implementation content:client authentication.

Document structure

Every application has a root directory,For example ch2;theoretically it can be placed anywhere,But need to be configured,Simple way,Put it directly under the webapps directory,Applications in this directory will be automatically loaded.

There will be a web-inf directory in the root directory. Files in this directory cannot be accessed remotely.It mainly stores configuration files, class files, and resource files.

The configuration file in web-inf is web.xml, and every web application will have such a configuration file.

There are two files in web-inf for storing class files and resource files.lib and classes, class libraries in the form of compressed jars are stored under lib,classes directly store class files (contains package information).

Page files (including jsp files, html files, and image files) can be placed under the root directory (ch2) or in the subfolders (cannot be placed in web-inf).

2, operation mode

Access method:

Prerequisite:Deploy the web application to the server,Start the server.

The following uses ch2.jsp as an example to introduce the access process:

1) The client sends a request through the browser;

2) The web server receives this request,Then transferred to the application server;

3) The application server will look for the file the client wants to access,Assume that the file accessed is ch2.jsp;there are two cases:

First visit:the application server converts the jsp file into a java file;then compiles it into a class file;then loads the class;Instantiate the object and initialize it;

Follow-up visit:The page object corresponding to the jsp file already exists,Find this object directly;

4) the application server encapsulates the request information,Then call the corresponding method;

5) The application server passes the method execution result (response to the content of the client) to the web server

6) The web server sends this result to the client;

7) The client browser parses the received html code into a web page.This is the result we see.

The following are the contents of several files during the operation.

Content of source file ch2.jsp:


<%= "ffffffffffffffff"%>

The converted file ch2_jsp.java:

package org.apache.jsp;
import javax.servlet. *;
import javax.servlet.http. *;
import javax.servlet.jsp. *;
public final class ch2_jsp extends org.apache.jasper.runtime.httpjspbase
  implements org.apache.jasper.runtime.jspsourcedependent {
 private static java.util.list _jspx_dependants;
 private javax.el.expressionfactory _el_expressionfactory;
 private org.apache.annotationprocessor _jsp_annotationprocessor;
 public object getdependants () {
  return _jspx_dependants;
 public void _jspinit () {
  _el_expressionfactory=jspfactory.getdefaultfactory (). getjspapplicationcontext (getservletconfig (). getservletcontext ()). getexpressionfactory ();
  _jsp_annotationprocessor=(org.apache.annotationprocessor) getservletconfig (). getservletcontext (). getattribute (org.apache.annotationprocessor.class.getname ());
 public void _jspdestroy () {
 public void _jspservice (httpservletrequest request, httpservletresponse response)
    throws java.io.ioexception, servletexception {
  jspfactory _jspxfactory=null;
  pagecontext pagecontext=null;
  httpsession session=null;
  servletcontext application=null;
  servletconfig config=null;
  jspwriter out=null;
  object page=this;
  jspwriter _jspx_out=null;
  pagecontext _jspx_page_context=null;
  try {
   _jspxfactory=jspfactory.getdefaultfactory ();
   response.setcontenttype ("text/html");
   pagecontext=_jspxfactory.getpagecontext (this, request, response,   null, true, 8192, true);
   application=pagecontext.getservletcontext ();
   config=pagecontext.getservletconfig ();
   session=pagecontext.getsession ();
   out=pagecontext.getout ();
   out.write ("dddddddddddddddddddddddddddd/r/n");
   out.print ("ffffffffffffffff");
  } catch (throwable t) {
   if (! (t instanceof skippageexception)) {
    if (out!=null&&out.getbuffersize ()!=0)
     try {out.clearbuffer ();} catch (java.io.ioexception e) {}
    if (_jspx_page_context!=null) _jspx_page_context.handlepageexception (t);
  } finally {
   if (_jspxfactory!=null) _jspxfactory.releasepagecontext (_jspx_page_context);

Information returned to the client (can be seen through the browser's view source file function):



3.Stateless request response mode

The user sends a request through the client,It can be requested via an address bar, hyperlink, button, or form element event.Regardless of how the request is sent,These request information will be encapsulated into an httpservletrequest object, and the server will use this object as a parameter to call the page object.This method will respond to the client after execution,The httpservletrequest object is then deleted.If the request is sent again,A new httpservletrequest object is created, and the information from the last visit is gone.

So the server will not save the information previously accessed by the client,This is called a stateless request response mode.

Next introduce the basic problem of jsp technology:input and output.First look at how to complete the input.

4.Input elements

Input is done via form elements.The common form elements are as follows:

1) form

To submit information,First you need a form, and only the information in the form can be submitted.

Start Identification<form>

End identifier</form>

The main attributes:

action attribute:is the location of the target file,Who to submit to

method attribute:request method,There are get and post

Note:forms cannot be nested.

2) Single line text box

Basic syntax format:

<input type="text" name="username" value="Please enter a user name">

type="text" indicates that this is a single-line text box;

name represents the name of the text box,Very important,The server needs to take the value according to the name;

value gives the initial value.

3) Password box

Basic syntax format:

<input type="password" name="userpass">

The usage is basically the same as for a single-line text box.

4) Hidden domain

Basic syntax format:

<input type="hidden" name="userpass">

Used to pass values ​​between multiple pages,It is basically the same as the current text box.

5) Radio button

Syntax format:

<input type="radio" name="sex" value="male">male

<input type="radio" name="sex" value="female">female

The names of a group of radio buttons should be consistent,This ensures that only one option is selected for multiple options.

Note:The content displayed after the radio button has nothing to do with the radio button.Just tell the user what this radio button means.

6) check box

Syntax format:

<input type="checkbox" name="fav" value="music">

<input type="checkbox" name="fav" value="sports">

The values ​​of the same set of checkboxes should also be consistent,The value can be taken in a uniform manner.

7) Drop-down list

Syntax format:

Start tag:<select name="select">

End tag:</select>

Each option in the drop-down box:<option value="1">displayed information</option>

Gender drop-down box:

<select name="sex">

<option value="male">male</option>

<option value="female">female</option>


8) Multi-line text field

Syntax format:

<textarea name="">sdsfsddddddddd</textarea>

To initialize a text field,You need to put the initial value between the start and end tags of the label.

Note:This is not the same as assigning other elements through the value attribute.

9) Submit button

<input type="submit" value="submit">

No name is usually needed.

10) Reset button

<input type="reset" value="reset">

No name is usually needed.

11) Ordinary button

Form submission can also be done via normal buttons,Need to write javascript code.

Syntax format:

<input type="button" value="submit" onclick="…">

5. Input example:registration page

Reference code register.jsp:

<%@page contenttype="text/html;charset=gb2312"%>
Please register<br>
<form method="post" name="fi1" action="process.jsp">
  User id:<input type="text" name="userid"><br>
  Password:<input type="password" name="userpass"><br>
  Confirm password:<input type="password" name="userpass1"><br>
  Gender:<input type="radio" name="sex" value="male" checked>male
     <input type="radio" name="sex" value="female">female<br>
  Hobbies:<input type="checkbox" name="fav" value="sports">sports
     <input type="checkbox" name="fav" value="music">music
     <input type="checkbox" name="fav" value="programming">programming<br>
    <select name="degree">
      <option value="Undergraduate"&Undergraduate</option>
      <option value="Master">Master</option>
      <option value="Specialist">Specialist</option>
      <option value="Doctor">Doctor</option>
    <textarea name="comment"></textarea><br>
    <input type="submit" value="submit"<input type="reset" value="reset">

This page can complete the submission of user information,When the user has finished typing and selecting, click the submit button.The browser sends this request to the server,According to the value of the action attribute in the form,We know that the server will call process.jsp for processing.Here's how to write process.jsp to get user input information.

6.Get information

When I introduced the operation principle earlier,Customer request information,Including entered and selected information,Will be encapsulated in the httpservletrequest object, so you only need to access this object in process.jsp,How do I get this object?

Several internal objects are provided in jsp,One of them is request, just use this object directly.For internal objects,We can use it directly,No need to declare and instantiate.

The request information can be obtained by the following two methods:

getparameter (element name)

getparametervalues ​​(element names)

The former is used to get the value of a single-valued element,For example, text boxes, radio buttons, password boxes, and so on.The latter is used to get the value of a multi-valued element,Examples are check boxes, list boxes that allow multiple selection.

7. Example:Registration information display

Source file of process.jsp:

<%@page contenttype="text/html;charset=gb2312"%>
The registration information is as follows:
  string userid=request.getparameter ("userid");
  string userpass=request.getparameter ("userpass");
  string userpass1=request.getparameter ("userpass1");
  string sex=request.getparameter ("sex");
  //encode gender
  sex=new string (sex.getbytes ("8859_1"));
  string [] fav=request.getparametervalues ​​("fav");
  //This method is used to get the value of a multi-valued element
  string degree=request.getparameter ("degree");
  string comment=request.getparameter ("comment");
  out.println ("userid:" + userid);
<br>Password:<%= userpass%>
<br>Confirm password:<%= userpass1%>
<br>Gender:<%= sex%>
  if (fav!=null)
  for (string s:fav)
   s=new string (s.getbytes ("8859_1"));
   out.print (s);
<br>Education:<%= degree%>
<br>Remarks:<%= comment%>

Note:This way of code (java code and html code nesting) is not recommended, here just to make this program more complete.But the code for getting values ​​and transcoding needs to be mastered,Will be used in the servlet in the future, there is no change.

8. Practice:Complete the book adding interface.The information added by the user is displayed again.

  • Previous JQuery entry basic small examples (1)
  • Next Explain the this pointer and constant object in C ++