在java代码中写文档(html),这是servlet;在文档中写java代码,这是jsp;而如何将两者分开,这就是MVC模式了。虽然是java的一小步,却是程序员们生活的一大步哦。
下面给出一个简单的用户登陆功能,采用jsp/servelt/javaBean来实现MVC模式(即经典的模式2)。
模型层:
package com.vitamin.DataAccess;
import java.sql.*;
public class DBbase {
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnstr = "jdbc:odbc:myDB";
Connection connect = null;
ResultSet rs = null;
Statement stmt = null;
public DBbase()
{
try
{
Class.forName(sDBDriver);
}
catch(ClassNotFoundException ex)
{
System.err.println(ex.getMessage());
}
}
public ResultSet executeQuery(String sql)
{
try
{
this.connect = DriverManager.getConnection(sConnstr);
this.stmt = this.connect.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println(ex.getMessage());
}
return rs;
}
public int executeUpdate(String sql)
{
int result = 0;
try
{
this.connect = DriverManager.getConnection(sConnstr);
this.stmt = this.connect.createStatement();
result = stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
System.err.println(ex.getMessage());
}
return result;
}
}
控制层:
package com.vitamin.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.vitamin.DataAccess.*;
import java.sql.*;
public class Hello extends HttpServlet {
/**
* Constructor of the object.
*/
public Hello() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("GBK");
String name = request.getParameter("name");
String pwd = request.getParameter("password");
DBbase myDb = new DBbase();
ResultSet rs = null;
int result = 0;
String sql = "select count(*) as count from users where username = '"+name+"' and password = '"+pwd+"'";
try
{
rs = myDb.executeQuery(sql);
if(rs.next())
{
result = rs.getInt("count");
}
}
catch(SQLException ex)
{
ex.printStackTrace();
}
if(result>0)
{
out.println("合法用户");
}
else
{
out.println("非法用户");
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
Web表示层:
<%@ 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 'index.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">
-->
</head>
<body>
<FORM name="form1" method="POST" action="/servletTest/servlet/Hello">
<P> 用户名: <INPUT type="text" name="name" maxlength="20" /></P>
<P> 密码 <INPUT type="password" name="password" maxlength="20" /> </P>
<P>
</P>
<P> <INPUT type="Submit" name="button4" value="确定" /> <INPUT type="Reset" name="button5" value="重置"></P>
<P>
</P>
<P>
</P>
</FORM>
</body>
</html>
看过一篇资料说,servlet能够与多个客户机连接,接受多个客户的输入,并将结果广播到多个客户机上,例如可以作为一个多人参与的游戏服务器,这点倒是让我挺吃惊的,不知道如何能够实现哪?如果是真的,那servlet是如何得知客户的详细信息的?难道可以象socket那样?不解。。。
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2006/06/25/435010.html,如需转载请自行联系原作者
微信关注我们
原文链接:https://yq.aliyun.com/articles/343366
转载内容版权归作者及来源网站所有!
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
相关文章
发表评论
资源下载
更多资源Mario,低调大师唯一一个Java游戏作品
马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。
Apache Tomcat7、8、9(Java Web服务器)
Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。
Eclipse(集成开发环境)
Eclipse 是一个开放源代码的、基于Java的可扩展开发平台。就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境。幸运的是,Eclipse 附带了一个标准的插件集,包括Java开发工具(Java Development Kit,JDK)。
Sublime Text 一个代码编辑器
Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。