开发JSP&Servlet
终于有空可以闲下来了,前2个星期忙着写代码。在开发过程中,因为不懂的东西太多,所以这段时间在不停的恶补,忙的连看美女的时间都没。就 趁着今天夜黑风高之际,我就继续写点把。介绍下我的开发工具是:eclipse3.1+myeclipse4.0GA 开发平台:win2000
( myeclipse4.0有个功能很不错,支持所见即所得,就是速度慢)
基本概念不说了,过下书就算了。我就用个用户登录的例子来说明如果使用JSP+SERVLET+JAVABEAN来开发项目,这个例子是通过读取XML文件来验证是否是合法用户。
先说一下流程:本来想画流程图的,但是机器里还没装VISIO。。。以后在补把。其实也很简单的。
这里是源码,需要的朋友可以下载看看 http://www.blogjava.net/Files/yuanhuaming/mvctest.rar
可能web.xml需要改一下,把还没修改好的传上去了。
Login.jsp
<%
@ page language="java" import="java.util.*" pageEncoding="UTF-8"
%>
< html >
< head >
< SCRIPT language ="JavaScript" >
function clean() {
form1.passWord.value = "";
form1.userName.value = "";
}
</ SCRIPT >
</ head >
< body >
< FORM name ="form1" action ="Login.do" method ="post" >
username: < INPUT name ="userName" type ="text" >< P ></ P >
password: < INPUT type ="password" name ="passWord" >< P ></ P >< P >
< INPUT type ="submit" name ="submit" value ="submit" >
< INPUT type ="Button" name ="reset" value ="reset" onclick ="clean()" >
</ P >< P ></ P ></ FORM >
</ body >
</ html >
通过action定向到Login.do, Login.do就是通过web.xml映射到指定的servlet。请看:
< html >
< head >
< SCRIPT language ="JavaScript" >
function clean() {
form1.passWord.value = "";
form1.userName.value = "";
}
</ SCRIPT >
</ head >
< body >
< FORM name ="form1" action ="Login.do" method ="post" >
username: < INPUT name ="userName" type ="text" >< P ></ P >
password: < INPUT type ="password" name ="passWord" >< P ></ P >< P >
< INPUT type ="submit" name ="submit" value ="submit" >
< INPUT type ="Button" name ="reset" value ="reset" onclick ="clean()" >
</ P >< P ></ P ></ FORM >
</ body >
</ html >
<
servlet
>
< servlet-name >Login </ servlet-name >//3
< servlet-class > com.yhm.servlet.Login </ servlet-class >//4
</ servlet >
< servlet-mapping >
< servlet-name > Login </ servlet-name >//2
< url-pattern > /Login.do </ url-pattern >//1
</ servlet-mapping >
上面标志的数字就是依次查找的顺序,很容易理解把。
< servlet-name >Login </ servlet-name >//3
< servlet-class > com.yhm.servlet.Login </ servlet-class >//4
</ servlet >
< servlet-mapping >
< servlet-name > Login </ servlet-name >//2
< url-pattern > /Login.do </ url-pattern >//1
</ servlet-mapping >
Login.java
package
com.yhm.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.yhm.bean.testConfig;
public class Login extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = new String(request.getParameter("userName").getBytes("iso8859-1"),"utf-8");
String password = new String(request.getParameter("passWord").getBytes("iso8859-1"),"utf-8");
testConfig config = new testConfig();
//.vaildate() return boolean
if(config.Vaildate(username, password) == true) {
/**//*通?SESSION?量?参
HttpSession session = request.getSession();
session.setAttribute("userName", username);
response.sendRedirect("welcome.jsp");
*/
//request?参
request.setAttribute("username", username);
//定向
RequestDispatcher dispatcher = request.getRequestDispatcher("welcome.jsp");
dispatcher.forward(request, response);
}
else {
response.sendRedirect("error.jsp");
}
}
//init and destroy
public void init() throws ServletException {}
public void destroy() {
super.destroy();
}
}
主要负责获取表单内的参数值,并且转向到指定的页面。
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.yhm.bean.testConfig;
public class Login extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = new String(request.getParameter("userName").getBytes("iso8859-1"),"utf-8");
String password = new String(request.getParameter("passWord").getBytes("iso8859-1"),"utf-8");
testConfig config = new testConfig();
//.vaildate() return boolean
if(config.Vaildate(username, password) == true) {
/**//*通?SESSION?量?参
HttpSession session = request.getSession();
session.setAttribute("userName", username);
response.sendRedirect("welcome.jsp");
*/
//request?参
request.setAttribute("username", username);
//定向
RequestDispatcher dispatcher = request.getRequestDispatcher("welcome.jsp");
dispatcher.forward(request, response);
}
else {
response.sendRedirect("error.jsp");
}
}
//init and destroy
public void init() throws ServletException {}
public void destroy() {
super.destroy();
}
}
ComJDom.java 用于封装如何访问XML
需要导入一个jdom.jar包,SDK里也有类似解析XML的包不过不太好用。
package
com.yhm.bean;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.jdom.Document;
import org.jdom.input.SAXBuilder;
public class ComJDom {
public static Document getDocument(String XMLUser) throws Exception {
Document dom = null;
InputStream instr = null;
try {
SAXBuilder build = new SAXBuilder();
instr = new FileInputStream(XMLUser);
dom = build.build(instr);
}
finally {
if (instr != null) {
try {
instr.close();
}
catch (IOException ex) {
throw ex;
}
}
}
return dom;
}
}
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.jdom.Document;
import org.jdom.input.SAXBuilder;
public class ComJDom {
public static Document getDocument(String XMLUser) throws Exception {
Document dom = null;
InputStream instr = null;
try {
SAXBuilder build = new SAXBuilder();
instr = new FileInputStream(XMLUser);
dom = build.build(instr);
}
finally {
if (instr != null) {
try {
instr.close();
}
catch (IOException ex) {
throw ex;
}
}
}
return dom;
}
}
testConfig.java 用来验证用户名是否正确
package
com.yhm.bean;
import java.util.ArrayList;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
public class testConfig {
private List userList = null;
private List pwdList = null;
public boolean Vaildate(String userName, String pwd) {
//验证用户名密码是否正确
for(int i=0;i<userList.size();i++){
if(userList.get(i).equals(userName) && pwdList.get(i).equals(pwd)){
return true;
}
} return false;
}
public testConfig() {
try {
userList = new ArrayList();
pwdList = new ArrayList();
String xmlStr = "F:/project by eclipse/mvctest/WEB-INF/test.xml";
Document dom = ComJDom.getDocument(xmlStr);
//得到根元素返回元素类型
Element root = dom.getRootElement();
//得到根元素的下一级元素
List list = root.getChildren();
Element action = null;
for (int i = 0; i < list.size(); i++) {
action = (Element) list.get(i);
//得到username元素里的内容
String username = action.getChildText("username");
String password = action.getChildText("password");
//放入2个List表中
userList.add(username);
pwdList.add(password);
}
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}
import java.util.ArrayList;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
public class testConfig {
private List userList = null;
private List pwdList = null;
public boolean Vaildate(String userName, String pwd) {
//验证用户名密码是否正确
for(int i=0;i<userList.size();i++){
if(userList.get(i).equals(userName) && pwdList.get(i).equals(pwd)){
return true;
}
} return false;
}
public testConfig() {
try {
userList = new ArrayList();
pwdList = new ArrayList();
String xmlStr = "F:/project by eclipse/mvctest/WEB-INF/test.xml";
Document dom = ComJDom.getDocument(xmlStr);
//得到根元素返回元素类型
Element root = dom.getRootElement();
//得到根元素的下一级元素
List list = root.getChildren();
Element action = null;
for (int i = 0; i < list.size(); i++) {
action = (Element) list.get(i);
//得到username元素里的内容
String username = action.getChildText("username");
String password = action.getChildText("password");
//放入2个List表中
userList.add(username);
pwdList.add(password);
}
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}
test.xml 记录了合法的用户名密码
<?
xml version="1.0" encoding="UTF-8"
?>
< userList >
< user >
< username > admin </ username >
< password > admin </ password >
</ user >
< user >
< username > test </ username >
< password > test </ password >
</ user >
< user >
< username > tiger </ username >
< password > tiger </ password >
</ user >
</ userList >
< userList >
< user >
< username > admin </ username >
< password > admin </ password >
</ user >
< user >
< username > test </ username >
< password > test </ password >
</ user >
< user >
< username > tiger </ username >
< password > tiger </ password >
</ user >
</ userList >
还有一张error.jsp ,有错误就会跳转到这里来。
总结:尽量不要把代码写在jsp里,尽量不要使用DW画界面,会出来很多垃圾代码,后期修改的时候看起来也累。通过这个例子,我似乎渐渐了解了为什么那么多的框架使用XML做配置文件及其如何工作的。
实际开发项目的时候会碰到很多很多奇怪而且难以捉摸的问题,可能这就是为什么要加班了。我前1个星期就碰到一个很奇怪的页面问题。本来显示正常,然后加了一个完全不相关的JSP文件,结果页面排版就出问题。然后COPY一下在其他机器上运行一切正常。郁闷了2天.....结果无意装了新版本的tomcat,把老版本卸了。然后一运行,正常Y_Y,谁会想到是tomcat的问题,而且tomcat也没报什么错。