67,538
社区成员
发帖
与我相关
我的任务
分享<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-template" prefix="template" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-nested" prefix="nested" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
<script type="text/javascript">
var req;
function UserNameCheck()
{
var username = document.getElementById('username').value;
var url = "/user.do?user_name=" + escape(username);
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if(req){
req.open("GET", url, true);
req.onreadystatechange = callback;
req.send(null);
}
}
function callback() {
if (req.readyState == 4) {
if (req.status == 200) {
parseMessage();
// update the HTML DOM based on whether or not message is valid
}else{
alert ("Not able to retrieve description" + req.statusText);
}
}
else
{
document.getElementById("check_username").innerHTML = "正在验证用户名....";
}
}
function parseMessage() {
var xmlDoc = req.responseXML.documentElement;
var node = xmlDoc.getElementsByTagName('info');
document.getElementById('check_username').innerHTML = node[0].firstChild.nodeValue;
}
function Form_Submit()
{
if(regForm.username.value=="")
{
alert("用户名不能为空!");
return false;
}
else if(regForm.password.value=="")
{
alert("密码不能为空!");
return false;
}
else if(regForm.password.value!=regForm.repassword.value)
{
alert("两次输入的密码不一致!");
return false;
}
regForm.submit();
}
</script>
<head>
<html:base />
<title>register.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body> <html:form styleId="regForm" method="post" action="/user.do">
<table width="70%" border="1">
<tr align="center">
<td colspan="2">
用户注册
</td>
</tr>
<tr>
<td width="24%" align="center">
用户名:
</td>
<td width="76%">
<input name="username" type="text" id="username" onBlur="UserNameCheck()">
<span id="check_username"></span>
</td>
</tr>
<tr>
<td align="center">
密码:
</td>
<td>
<input name="password" type="password" id="password">
</td>
</tr>
<tr>
<td align="center">
重复密码:
</td>
<td>
<input name="repassword" type="password" id="repassword">
</td>
</tr>
<tr>
<td align="center">
email:
</td>
<td>
<input name="email" type="text" id="email">
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="button" name="Submit" value="按钮" onClick="Form_Submit()">
</td>
</tr>
</table>
</html:form>
</body>
</html:html>
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package org.marriager.action;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.marriager.form.UserForm;
/**
* MyEclipse Struts
* Creation date: 11-03-2008
*
* XDoclet definition:
* @struts.action path="/user" name="userForm" scope="request" validate="true"
*/
public class UserAction extends Action {
/*
* Generated Methods
*/
public String[] usernameList;
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws IOException
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws IOException {
UserForm userForm = (UserForm) form;
System.out.println("我的");
response.setContentType("text/xml;charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
String username = (String) request.getParameter("user_name");
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
if (username.equals("") || username == null) {
xml +="<message><info>Username is required !</info></message>";
}else if(this.IsContain(username))
{
xml += "<message><info>用户名已注册!</info></message>";
}
else
{
xml += "<message><info>合法用户名!</info></message>";
}
response.getWriter().write(xml);
return null;
}
public void init(ServletConfig config) throws ServletException {
usernameList = new String[] { "Tom", "Jerry", "Brain" };
}
public boolean IsContain(String param) {
for (int i = 0; i < usernameList.length; i++) {
if (usernameList[i].equals(param)) {
return true;
} else
continue;
}
return false;
}
}<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="userForm" type="org.marriager.form.UserForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="userForm"
name="userForm"
path="/user"
scope="request"
type="org.marriager.action.UserAction" />
</action-mappings>
<message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>