2. Simple login and login Functions

Source: Internet
Author: User

2. Simple login and login Functions

1. Create in the WebContent directory

Login. jsp

1 <% @ page language = "java" import = "java. util. *" pageEncoding = "GB2312" %> 2 <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 3 

Error. jsp

1 <% @ page language = "java" import = "java. util. *" pageEncoding = "GB2312" %> 2 <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 3 

Welcome. jsp

1 <% @ page language = "java" import = "java. util. *" pageEncoding = "GB2312" %> 2 <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 3 

2. The configuration file Struts. xml must also be provided.

This file can be placed in the src directory, that is, the Java Resources/src directory in Eclipse, or under the WebContent/WEB-INF/classes/directory, because when eclipse deploys a web project, the struts. the xml file is stored in the classes directory. In addition, this file is not found in the project created by eclipse and needs to be created manually.

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <! DOCTYPE struts PUBLIC 3 "-// Apache Software Foundation // DTD Struts Configuration 2.0 // EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 <struts> 6 <package name = "test" extends = "struts-default"> 7 <! -- ActionClass defines the ing between views and physical resources --> 8 <action name = "login" class = "com. suo. loginAction "method =" execute "> 9 <result name =" success "> welcome. jsp </result> 10 <result name = "error"> error. jsp </result> 11 </action> 12 </package> 13 </struts>

3. In login. jsp, the action of the form is specified as login, and the class is com. suo. LoginAction. Therefore, the com. suo package is created under src, And the LoginAction class is set in the package.

1 package com. suo; 2 import com. opensymphony. xwork2.ActionSupport; 3 // typically inherits ActionSupport 4 public class LoginAction extends ActionSupport {5 private String username; 6 private String userpass; 7 8 public String execute () {9 if ("123456 ". equals (username) & "123456 ". equals (userpass) 10 return SUCCESS; 11 else12 return ERROR; 13} 14 15 public String getUsername () {16 return username; 17} 18 public void setUsername (String username) {19 this. username = username; 20} 21 public String getUserpass () {22 return userpass; 23} 24 public void setUserpass (String userpass) {25 this. userpass = userpass; 26} 27}

The code is complete. Check the effect!

Right-click login. jsp, Run as-> run on Server

 

 

Enter the username and password 123456. OK!

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.