jsp servlet推荐读物: java servlet & jsp cookbook, Bruce W. Perry, o'reilly,
---------------------------------------------
Http session:It's almost impossible to visit any interactive web site today that does not make use of the HTTP session API. By providing multiple options for tracking a series of requests and associating those requests with a specific user, HTTP session allows applications to appear dynamic to application users. The most often cited example of HTTP session is the creation of a "shopping cart" for shoppers on a web site. In this example, information associating the user and their prior navigation through the web site and their selections are stored as objects in HTTP session. Once the users are ready to check out from the web site and purchase their selections, the application typically constructs a page composed of all the selected items stored in the "shopping cart."1 By maintaining application state between browser requests, HTTP session overcomes the default stateless behavior for HTTP requests.
The HTTP session API component of the Java Servlet specification provides a mechanism for web applications to maintain a user's state information, and this mechanism addresses some of the problems with other options for maintaining state, such as those based solely on cookies. This mechanism, known as a session, allows a web-application developer to maintain user state information on the server, while passing minimal information back to the user to track the session via one of three options: cookies, URL encoding, and SSL sessions.
------------------------------------------------------- -------------------------- ---------------------
J2EE pathfinder: The many uses of implicit objects:http://www-128.ibm.com/developerworks/library/j-pj2ee7.html(jsp中有一些隐式的对象存在)
---------------------------------------------
Ant: http://ant.apache.org/manual/index.html
---------------------------------------------
tomcat乱码问题:
有三种乱码的情况:
1页面乱码
a浏览器显示不正确,view->source,如果看到源码中没有乱码,这就是浏览器显示的问题了。
解决:view->encoding选择正确的编码方式。或者在将页面发送过来的时候,就将其contentType设置为text/html;charset=gb2312
b在后台产生动态页面时就是乱码,这是情况2和3了。
2用户提交后获得的数据是乱码
当后台用request对象获取到用户的输入后,再输出到控制台就成了乱码,那就是因为request在解析用户输入的时候的解码方式不对。可以用request.setCharacterEncoding来解决。对于大量的页面通过过滤器(filter)的方式来解决。
(资料,filter是个广泛在web使用的机制)Filter code with Servlet 2.3 model:
http://www.javaworld.com/javaworld/jw-06-2001/jw-0622-filters.html
一个filter的代码:(java数据库高级编程宝典,陈天河)
package jdbcbook.pub.filters;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.UnavailableException;
/**
* 设置Tomcat的正确编码格式
*/
public class SetCharacterEncodingFilter implements Filter
{
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destroy( )
{
this.encoding = null;
this.filterConfig = null;
}
public void doFilter( ServletRequest request,
ServletResponse response,
FilterChain chain )
throws IOException, ServletException
{
// Conditionally select and set the character encoding to be used
if ( !ignore || (request.getCharacterEncoding() == null) )
{
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
// Pass control on to the next filter
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException
{
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = false;
else if (value.equalsIgnoreCase("false"))
this.ignore = true;
else if (value.equalsIgnoreCase("no"))
this.ignore = true;
else
this.ignore = false;
}
protected String selectEncoding(ServletRequest request)
{
return (this.encoding);
}
}
3将数据保存到数据库后出现乱码
这种情况处理比较灵活,没有固定处理模式。
总结:对于乱码的处理,关键是找到乱码的源头,可以按照下面的步骤检查:用户端的显示,接受用户的数据,保存到数据库,从数据库读取数据,将数据库数据显示给用户。
------------------------------------------------
JSPX: XML based jsp
符合XML语法的jsp,在jsp规范中有定义。
这里是一个例子的链接:http://cvs.mmbase.org/viewcvs/applications/richtext/templates/import/index.jspx?view=markup
一个说明:http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPX3.html
其中,使用<jsp:scriptlet></jsp:scriptlet>时,注意:
如果使用了语句
out.println("<br>"),则一定要有一个对应的
out.println("</br>"),这样才符合XML的规范。
『
上面这段话好像不对,
有如下文档:
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XML Explorer v2.0 by Mergesoft (www.mergesoft.com)-->
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jstl/core" version="2.0">
<jsp:directive.page contentType="text/html; charset=gb2312"></jsp:directive.page>
<jsp:directive.page import="java.sql.*"></jsp:directive.page>
<html></html>
<head></head>
<body>
<jsp:scriptlet>
Class.forName("net.sourceforge.jtds.jdbc.Driver");
String url="jdbc:jtds:sqlserver://172.18.136.131:1433/pubs";
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from titles";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
</jsp:scriptlet>
您的第一个字段内容为:
<jsp:scriptlet>
out.println(rs.getString(1));
</jsp:scriptlet>
您的第二个字段内容为:
<jsp:scriptlet>
out.println(rs.getString(2));
out.println( "<br></br>");
}
out.println("数据库操作成功,恭喜你");
rs.close();
stmt.close();
conn.close();
</jsp:scriptlet>
</body>
</jsp:root>
总是报错:元素体里不能有xml标记(大概这个意思)
改为:
。。。
<jsp:scriptlet>
out.println(rs.getString(2));
</jsp:scriptlet>
<br></br>
<jsp:scriptlet>
}
。。。
才能运行。
』
这是在<jsp:scriptlet></jsp:scriptlet>中出现的html标记要遵守XML的例子(从上面的例子看,是不是不能出现Html标记,待考),同样在jspx中其他地方(如<jsp:root></jsp:root>)出现的html标记也要遵守XML,所以象<img>之类没有结束符的,都要有结束符</img>。
总之,jspx中的jsp是一种xml。
另外,jspx文件也可以以jsp为扩展名,<jsp:root></jsp:root>标记是区别jsp和jspx的标准。