Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java EE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 petstore中templateServlet原码解读,请诸位老大帮忙看看
wxbmailbox





发贴: 6
积分: 0
于 2003-04-23 15:04 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
public class TemplateServlet extends HttpServlet {
private HashMap allScreens;
private ServletConfig config;
private ServletContext context;
private String defaultLocale;
private boolean cachePreviousScreenAttributes = false;
private boolean cachePreviousScreenParameters = false;
private static final String PREVIOUS_SCREEN = "PREVIOUS";

public void init(ServletConfig config) throws ServletException {
/*调用此servlet的时候首先执行这个方法,没错吧*/
super.init(config);
this.config = config;
this.context = config.getServletContext();
// enable the caching of previous screen attributes
String cachePreviousScreenAttributesString = config.getInitParameter("cache_previous_screen_attributes");
/*自始至终我没看见哪个地方有这个变量值,也就是cachePreviousScreenAttributesString肯定是空的,不知有何用意*/
if (cachePreviousScreenAttributesString != null) {
if (cachePreviousScreenAttributesString.toLowerCase().equals("true")) {
System.out.println("TemplateServlet: Enabled caching of previous screen attributes.");
cachePreviousScreenAttributes = true;
}
}
// enable the caching of previous screen parameters
String cachePreviousScreenParametersString = config.getInitParameter("cache_previous_screen_parameters");
/*同cachePreviousScreenAttributesString*/
if (cachePreviousScreenParametersString != null) {
if (cachePreviousScreenParametersString.toLowerCase().equals("true")) {
System.out.println("TemplateServlet: Enabled caching of previous screen parameters.");
cachePreviousScreenParameters = true;
}
}
allScreens = new HashMap();
defaultLocale = config.getInitParameter("default_locale");
if (defaultLocale == null) {
defaultLocale = (Locale.getDefault()).toString();
}
/*此时defaultLocale=en_US*/
String locales = config.getInitParameter("locales");
if (locales != null) {
StringTokenizer strTok = new StringTokenizer(locales,",");
/*此时locales=en_US,ja_JP,zh_CN*/
while (strTok.hasMoreTokens()) {
initScreens(config.getServletContext(), strTok.nextToken());
/*根据三种不同的语言分别调用initScreens,initScreens主要是将allScreens.put(language, screenDefinitions),这儿它为什么要一次将不同语种的全部screen全部读进来呢?*/
/*至此,init完成,开始process*/
}
}
}

private void initScreens(ServletContext context, String language) {
URL screenDefinitionURL = null;
try {
screenDefinitionURL = context.getResource("/WEB-INF/screendefinitions_" + language + ".xml");
} catch (java.net.MalformedURLException ex) {
System.err.println("TemplateServlet: malformed URL exception: " + ex);
}
if (screenDefinitionURL != null) {
Screens screenDefinitions = ScreenDefinitionDAO.loadScreenDefinitions(screenDefinitionURL);
if (screenDefinitions != null) {
allScreens.put(language, screenDefinitions);
} else {
System.err.println("Template Servlet Error Loading Screen Definitions: Confirm that file at URL /WEB-INF/screendefinitions_" + language + ".xml contains the screen definitions");
}
} else {
System.err.println("Template Servlet Error Loading Screen Definitions: URL /WEB-INF/screendefinitions_" + language + ".xml not found");
}
}

public void doPost (HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
process(request, response);
}

public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
process(request, response);
}

public void process (HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

String screenName = null;
String localeString = null;
String selectedUrl = request.getRequestURI();
if (request.getSession().getAttribute(WebKeys.CURRENT_URL) != null) {
request.getSession().removeAttribute(WebKeys.CURRENT_URL);
}
/*这个if语句快有什么作用?从哪儿取值,为什么取出来以后又要remove掉呢?*/

String languageParam = request.getParameter("locale");
/*这个local从哪儿取呢?*/
// The language when specified as a parameter overrides the language set in the session
if (languageParam != null) {
localeString = languageParam;
} else if (request.getSession().getAttribute(WebKeys.LOCALE) != null) {
localeString = ((Locale)request.getSession().getAttribute(WebKeys.LOCALE)).toString();
/*这个又是从哪儿取的呢?*/
}
if (allScreens.get(localeString) == null) {
localeString = defaultLocale;
/*这儿的localString值是多少*/
}

// get the screen name
int lastPathSeparator = selectedUrl.lastIndexOf("/") + 1;
int lastDot = selectedUrl.lastIndexOf(".");
if (lastPathSeparator != -1 && lastDot != -1 && lastDot > lastPathSeparator) {
screenName = selectedUrl.substring(lastPathSeparator, lastDot);
/*这儿的screenName是多少?*/
}
// check if request is for the previous screen
/*这个if语句块有什么作用?以下的看不懂了,请大家帮忙*/
if (screenName.equals(PREVIOUS_SCREEN)) {
String longScreenName = (String)request.getSession().getAttribute(WebKeys.PREVIOUS_SCREEN);
int lastDot2 = longScreenName.lastIndexOf(".");
if ( lastDot2 != -1 && lastDot2 > 0) {
screenName = longScreenName.substring(0, lastDot2);
}
// put the request attributes stored in the session in the request
if (cachePreviousScreenParameters) {
Map previousParams = (Map)request.getSession().getAttribute(WebKeys.PREVIOUS_REQUEST_PARAMETERS);
Map params = (Map)request.getParameterMap();
Iterator it = previousParams.keySet().iterator();
while (it.hasNext()) {
String key = (String)it.next();
Object value = previousParams.get(key);
params.put(key,value);
}
}
// put in the previous request attributes
if (cachePreviousScreenAttributes) {
Map previousAttributes = (Map)request.getSession().getAttribute(WebKeys.PREVIOUS_REQUEST_ATTRIBUTES);
Iterator it2 = previousAttributes.keySet().iterator();
// put the request attributes stored in the session in the request
while (it2.hasNext()) {
String key = (String)it2.next();
Object value = previousAttributes.get(key);
request.setAttribute(key,value);
}
}
} else {
String extension = selectedUrl.substring(lastDot, selectedUrl.length());
request.getSession().setAttribute(WebKeys.PREVIOUS_SCREEN, screenName + extension);
if (cachePreviousScreenParameters) {
// copy all the parameters into a new map
HashMap newParams = new HashMap();
Map params = (Map)request.getParameterMap();
Iterator it = params.keySet().iterator();
// put the request attributes stored in the session in the request
while (it.hasNext()) {
String key = (String)it.next();
Object value = params.get(key);
newParams.put(key,value);
}
request.getSession().setAttribute(WebKeys.PREVIOUS_REQUEST_PARAMETERS, newParams);
}
if (cachePreviousScreenAttributes) {
// put the request attributes into a map
HashMap attributes = new HashMap();
Enumeration enum = request.getAttributeNames();
while (enum.hasMoreElements()) {
String key = (String)enum.nextElement();
Object value = request.getAttribute(key);
attributes.put(key, value);
}
request.getSession().setAttribute(WebKeys.PREVIOUS_REQUEST_ATTRIBUTES, attributes);
}
}
// get the screen information for the coresponding language
Screen screen = null;
if (screenName != null){
Screens localeScreens = (Screens)allScreens.get(localeString);
if (localeScreens != null) screen = (Screen)localeScreens.getScreen(screenName);
// default to the default locale screen if it was not defined in the locale specific definitions
if (screen == null) {
System.err.println("Screen not Found loading default from " + defaultLocale);
localeScreens = (Screens)allScreens.get(defaultLocale);
screen = (Screen)localeScreens.getScreen(screenName);
}
if (screen != null) {
request.setAttribute(WebKeys.CURRENT_SCREEN, screen);
String templateName = localeScreens.getTemplate(screenName);
if (templateName != null) {
insertTemplate(request, response, templateName);
}
} else {
response.setContentType("text/html;charset=8859_1");
PrintWriter out = response.getWriter();
out.println("<font size=+4>Error:</font><br>Definition for screen " + screenName + " not found");
}
}
}

private void insertTemplate(HttpServletRequest request,
HttpServletResponse response,
String templateName)
throws IOException, ServletException {

// This method tries to wrap the request dispatcher call with-in
// a transaction for making the local EJB access efficient. However,
// it is not a fatal error, if such a transaction can not be started
// for some reason, so it handles them gracefully.

boolean tx_started = false;
UserTransaction ut = null;

try {
// Lookup the UserTransaction object
InitialContext ic = new InitialContext();
ut = (UserTransaction) ic.lookup("java:comp/UserTransaction");
ut.begin(); // start the transaction.
tx_started = true;
} catch (NamingException ne) {
// it should not have happened, but it is a recoverable error.
// Just dont start the transaction.
ne.printStackTrace();
} catch (NotSupportedException nse) {
// Again this is a recoverable error.
nse.printStackTrace();
} catch (SystemException se) {
// Again this is a recoverable error.
se.printStackTrace();
}

try {
context.getRequestDispatcher(templateName).forward(request, response);
} finally {
// commit the transaction if it was started earlier successfully.
try {
if (tx_started && ut != null) {
ut.commit();
}
} catch (IllegalStateException re) {
re.printStackTrace();
} catch (RollbackException re) {
re.printStackTrace();
} catch (HeuristicMixedException hme) {
hme.printStackTrace();
} catch (HeuristicRollbackException hre) {
hre.printStackTrace();
} catch (SystemException se) {
se.printStackTrace();
}
}
}
}


why edited on 2003-04-23 19:48


话题树型展开
人气 标题 作者 字数 发贴时间
3578 petstore中templateServlet原码解读,请诸位老大帮忙看看 wxbmailbox 11961 2003-04-23 15:04

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923