Eclipse + Struts + Ajax

Source: Internet
Author: User

Page: example. jsp
<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "GBK" %>
<%
String Path = request. getcontextpath ();
String basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/";
%>

<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Base href = "<% = basepath %>">
<Title> my JSP 'example. jsp 'starting page </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">
-->
<SCRIPT>

VaR req;
VaR which;
Function retrieveurl (URL ){
If (window. XMLHttpRequest) {// non-ie browsers
Req = new XMLHttpRequest ();
Req. onreadystatechange = processstatechange;
Try {
Req. Open ("get", URL, true );
} Catch (e ){
Alert ('object creation failed ');
}
Req. Send (null );
} Else if (window. activexobject) {// IE
Req = new activexobject ("Microsoft. XMLHTTP ");
If (req ){
Req. onreadystatechange = processstatechange;
Req. Open ("get", URL, true );
Req. Send (null );
}
}
}
Function processstatechange (){
If (req. readystate = 4) {// complete
If (req. Status = 200) {// OK response
Document. getelementbyid ("test"). innerhtml = Req. responsetext;
} Else {
Alert ("problem:" + Req. statustext );
}
}
}
</SCRIPT>
</Head>
<Body onload = "retrieveurl ('/test/example. Do')">
<Dir id = "test"> </dir>
</Body>
</Html>
Action part: exampleaction. Java
/*
* Generated by myeclipse struts
* Template path: templates/Java/javaclass. VTL
*/
Package com. SBT. Struts. Action;

Import java. Io. ioexception;
Import java. Io. printwriter;

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;

/**
* Myeclipse struts
* Creation date: 01-11-2008
*
* XDoclet definition:
* @ Struts. Action validate = "true"
*/
Public class exampleaction extends action {
/*
* Generated Methods
*/

/**
* 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 {
Response. setcontenttype ("text/html; charset = GBK ");
Printwriter out = response. getwriter ();
Out. println ("Hello world! ");
Out. Flush ();
Return NULL;
}
}
Config part: struts-config.xml

<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype Struts-config public "-// Apache Software Foundation // DTD struts configuration 1.1 //" http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd ">

<Struts-config>
<Data-sources/>
<Form-beans/>
<Global-exceptions/>
<Global-forwards/>
<Action-mappings>
<Action Path = "/example" type = "com. SBT. Struts. Action. exampleaction"/>

</Action-mappings>

<Message-resources parameter = "com. SBT. Struts. applicationresources"/>
</Struts-config>

Web Part: Web. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app xmlns = "http://java.sun.com/xml/ns/j2ee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" version = "2.4" xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<Servlet>
<Servlet-Name> action </servlet-Name>
<Servlet-class> org. Apache. Struts. Action. actionservlet </servlet-class>
<Init-param>
<Param-Name> config </param-Name>
<Param-value>/WEB-INF/struts-config.xml </param-value>
</Init-param>
<Init-param>
<Param-Name> debug </param-Name>
<Param-value> 3 </param-value>
</Init-param>
<Init-param>
<Param-Name> detail </param-Name>
<Param-value> 3 </param-value>
</Init-param>
<Load-on-startup> 0 </load-on-startup>
</Servlet>
<Servlet-mapping>
<Servlet-Name> action </servlet-Name>
<URL-pattern> *. DO </url-pattern>
</Servlet-mapping>
</Web-app>

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.