<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML>
JSON can be used to directly read the attribute values in the action instead of the out. Print (...) in the servlet (...)
Package COM. ajax. action; import COM. opensymphony. xwork2.actionsupport; public class ajaxaction extends actionsupport {private string username; // to reduce the card surface, add the get, Set Method private string password; private string MSG; Public String execute () {If (username. equals ("admin") & password. equals ("admin") {MSG = "Hello, login successfully";} else {MSG = "Login fail" ;}return "success ";}}
Then, in the XML file, set the type of the result to JSON, which corresponds to the ype in the previous. Post (), and inherit the JSON-Default
<package name="ajax" namespace="/" extends="json-default"><action name="ajax" class="com.ajax.action.AjaxAction"><result type="json"></result></action></package>