如何用Java实现Web服务器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用Java实现Web服务器相关的知识,希望对你有一定的参考价值。
参考技术A一 HTTP协议的作用原理
WWW是以Internet作为传输媒介的一个应用系统 WWW网上最基本的传输单位是Web网页 WWW的工作基于客户机/服务器计算模型 由Web 浏览器(客户机)和Web服务器(服务器)构成 两者之间采用超文本传送协议(HTTP)进行通信 HTTP协议是基于TCP/IP协议之上的协议 是Web浏览器和Web服务器之间的应用层协议 是通用的 无状态的 面向对象的协议 HTTP协议的作用原理包括四个步骤
( ) 连接 Web浏览器与Web服务器建立连接 打开一个称为socket(套接字)的虚拟文件 此文件的建立标志着连接建立成功
( ) 请求 Web浏览器通过socket向Web服务器提交请求 HTTP的请求一般是GET或POST命令(POST用于FORM参数的传递) GET命令的格式为
GET 路径/文件名 HTTP/
文件名指出所访问的文件 HTTP/ 指出Web浏览器使用的HTTP版本
( ) 应答 Web浏览器提交请求后 通过HTTP协议传送给Web服务器 Web服务器接到后 进行事务处理 处理结果又通过HTTP传回给Web浏览器 从而在Web浏览器上显示出所请求的页面
例 假设客户机与 /mydir/l建立了连接 就会发送GET命令 GET /mydir/l HTTP/ 主机名为的Web服务器从它的文档空间中搜索子目录mydir的文件l 如果找到该文件 Web服务器把该文件内容传送给相应的Web浏览器
为了告知 Web浏览器传送内容的类型 Web服务器首先传送一些HTTP头信息 然后传送具体内容(即HTTP体信息) HTTP头信息和HTTP体信息之间用一个空行分开
常用的HTTP头信息有
① HTTP OK
这是Web服务器应答的第一行 列出服务器正在运行的HTTP版本号和应答代码 代码 OK 表示请求完成
② MIME_Version
它指示MIME类型的版本
③ content_type 类型
这个头信息非常重要 它指示HTTP体信息的MIME类型 如 content_type text/指示传送的数据是html文档
④ content_length 长度值
它指示HTTP体信息的长度(字节)
( ) 关闭连接 当应答结束后 Web浏览器与Web服务器必须断开 以保证其它Web浏览器能够与Web服务器建立连接
二 Java实现Web服务器功能的程序设计
根据上述HTTP协议的作用原理 实现GET请求的Web服务器程序的方法如下
( ) 创建ServerSocket类对象 监听端口 这是为了区别于HTTP的标准TCP/IP端口 而取的
( ) 等待 接受客户机连接到端口 得到与客户机连接的socket
( ) 创建与socket字相关联的输入流instream和输出流outstream
( ) 从与socket关联的输入流instream中读取一行客户机提交的请求信息 请求信息的格式为 GET 路径/文件名 HTTP/
( ) 从请求信息中获取请求类型 如果请求类型是GET 则从请求信息中获取所访问的HTML文件名 没有HTML文件名时 则以l作为文件名
( ) 如果HTML文件存在 则打开HTML文件 把HTTP头信息和HTML文件内容通过socket传回给Web浏览器 然后关闭文件 否则发送错误信息给Web浏览器
( ) 关闭与相应Web浏览器连接的socket字
下面的程序是根据上述方法编写的 可实现多线程的Web服务器 以保证多个客户机能同时与该Web服务器连接
程序 WebServer java文件
//WebServer java 用JAVA编写Web服务器
import java io *
import *
public class WebServer
public static void main(String args[])
int i= PORT=
ServerSocket server=null
Socket client=null
try
server=new ServerSocket(PORT)
System out println( Web Server is listening on port +server getLocalPort())
for ( ) client=server accept() //接受客户机的连接请求
new ConnectionThread(client i) start()
i++
catch (Exception e) System out println(e)
/* ConnnectionThread类完成与一个Web浏览器的通信 */
class ConnectionThread extends Thread
Socket client //连接Web浏览器的socket字
int counter //计数器
public ConnectionThread(Socket cl int c)
client=cl
counter=c
public void run() //线程体
try
String destIP=client getInetAddress() toString() //客户机IP地址
int destport=client getPort() //客户机端口号
System out println( Connection +counter+ connected to +destIP+ on port +destport+ )
PrintStream outstream=new PrintStream(client getOutputStream())
DataInputStream instream=new DataInputStream(client getInputStream())
String inline=instream readLine() //读取Web浏览器提交的请求信息
System out println( Received +inline)
if (getrequest(inline)) //如果是GET请求
String filename=getfilename(inline)
File file=new File(filename)
if (file exists()) //若文件存在 则将文件送给Web浏览器
System out println(filename+ requested )
outstream println( HTTP/ OK )
outstream println( MIME_version )
outstream println( Content_Type text/ )
int len=(int)file length()
outstream println( Content_Length +len)
outstream println( )
sendfile(outstream file) //发送文件
outstream flush()
else //文件不存在时
String notfound=
Error file not found
outstream println( HTTP/ no found )
outstream println( Content_Type text/ )
outstream println( Content_Length +notfound length()+ )
outstream println( )
outstream println(notfound)
outstream flush()
long m =
while (m < ) m ++ //延时
client close()
catch (IOException e)
System out println( Exception +e)
/* 获取请求类型是否为 GET */
boolean getrequest(String s)
if (s length()> )
if (s substring( ) equalsIgnoreCase( GET )) return true
return false
/* 获取要访问的文件名 */
String getfilename(String s)
String f=s substring(s indexOf(′ ′)+ )
f=f substring( f indexOf(′ ′))
try
if (f charAt( )==′/′)
f=f substring( )
catch (StringIndexOutOfBoundsException e)
System out println( Exception +e)
if (f equals( )) f= l
return f
/*把指定文件发送给Web浏览器 */
void sendfile(PrintStream outs File file)
try
DataInputStream in=new DataInputStream(new FileInputStream(file))
int len=(int)file length()
byte buf[]=new byte[len]
in readFully(buf)
outs write(buf len)
outs flush()
in close()
catch (Exception e)
System out println( Error retrieving file )
System exit( )
程序中的ConnectionThread线程子类用来分析一个Web浏览器提交的请求 并将应答信息传回给Web浏览器 其中 getrequest()方法用来检测客户的请求是否为 GET getfilename(s)方法是从客户请求信息s中获取要访问的HTML文件名 sendfile()方法把指定文件内容通过socket传回给Web浏览器
对上述程序的getrequest()方法和相关部分作修改 也能对POST请求进行处理
三 运行实例
为了测试上述程序的正确性 将编译后的WebServer class ConnectionThread class和下面的l文件置于网络的某台主机的同一目录中(如 主机NT SRV的C JWEB目录)
程序 l文件
这是用JAVA写出的WEB服务器主页
年 月 日
首先在该主机上用java命令运行WebServer class
C jweb>java webserver
然后在客户机运行浏览器软件 在URL处输入WebServer程序所属的URL地址(如 ) 就在浏览器窗口显示出指定的HTML文档
注意 不能缺省端口号 如缺省 则运行该主机的正常WEB服务器
lishixinzhi/Article/program/Java/hx/201311/26626
在java Web中如何用Ajax实现用户名已存在
我是初学者,请高手提供代码,包括java类,和jsp页面的代码,急啊~~~~!!
我给你做一个例子:希望能帮到你。实现的功能:注册页面上当输入“lixin”时,显示该用户已被注册。其他的名称无所谓。希望能帮到你。欢迎追问。
一个简单的jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" language="javascript">
//根据浏览器的不同创建不同的XMLHttpRequest
function createXmlHttpRequest()
var xmlreq=false;
if(window.XMLHttpRequest)
xmlreq=new XMLHttpRequest();
else if(window.ActiveXobject)
try
xmlreq = new ActiveXobject("Msxm12.XMLHTTP");
catch(e1)
try
xmlreq = new ActiveXobject("Miscoft.XMLHTTP");
catch(e2)
return xmlreq;
//
function usernameCheck()
var username = document.all.username.value;//获得text的值
var request = createXmlHttpRequest();//创建request的对象
request.open("post","servlet/ValidationServlet?username="+username);
request.send();
request.onreadystatechange = function()
if(request.readyState==4&request.status==200)
var value = request.responseText;
if(value=="true")
document.all.unc.innerHTML="该用户名已经被注册";
else
document.all.unc.innerHTML="该用户可以注册";
</script>
</head>
<body>
用户姓名:<input type ="text" name="username" onblur="usernameCheck()" /><font color="red" size="-1" id="unc"></font>
<br>
用户密码:<input type ="password" name= "userpw" />
</body>
</html>
用到的Servlet:
package sample;
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;
public class ValidationServlet extends HttpServlet
/**
* 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
this.doPost(request, response);
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
response.setContentType("text/html");
String username = request.getParameter("username");
if(username.equals("lixin"))
response.getWriter().print("true");
else
response.getWriter().print("false");
参考技术A 初学者??这些是我学java一年才开始学的东西,这些东西太多了,都只能说一下思路,写代码,太长,要写好几百行,你写一个dao方法,按照用户名从数据库查询,如果不为空则就是用户名存在,反之不存在,既然你是初学者,我用其他快捷的方式完成了你也看不懂,最基础的太长,我不想写,所以你还是在网上找些例子看着学习吧,别人完成的毕竟没有自己完成的理解深刻 参考技术B 数据库操作类(mysql数据库)1.import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class BaseDao
protected Connection conn;
protected Statement sta;
protected ResultSet rs;
protected PreparedStatement ps;
public void setConnection()
try
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/dianlv";
conn = DriverManager.getConnection(url,"root","root");
catch (ClassNotFoundException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (SQLException e)
// TODO Auto-generated catch block
e.printStackTrace();
public void closeAll()
try
if(rs!=null)
rs.close();
if(ps!=null)
ps.close();
if(sta!=null)
sta.close();
if(conn!=null)
conn.close();
catch(SQLException e)
e.printStackTrace();
2.UserDao类集成baseDao类,实现对数据库中的数据名字超找:import java.sql.SQLException;public class UserDao extends BaseDao
/**
* 验证用户名是否成功;如果成功了返回true;
*
* **/
public boolean checkUserName(String userName)
boolean flag = false;
this.setConnection();
String sql ="select * from users where userName = ?";
try
ps = conn.prepareStatement(sql);
ps.setString(1,userName);
rs = ps.executeQuery();
if(rs.next())
flag = true;
catch (SQLException e)
// TODO Auto-generated catch block
e.printStackTrace();
return flag;
3.上面代码实现应该有一个user实体:创建user实体类:public class User
private String userName;
private int id;
private String password;
public String getUserName()
return userName;
public void setUserName(String userName)
this.userName = userName;
public int getId()
return id;
public void setId(int id)
this.id = id;
public String getPassword()
return password;
public void setPassword(String password)
this.password = password;
4.写一个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.fz.dao.UserDao;public class CheckUserNameServlet extends HttpServlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException doPost(request, response);
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
5:jsp页面用于用户的操作,上面用到ajax技术用户验证用户名是否重复<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'Login.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">
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
function checkUnique()
var content = $('#userName').val(); //获取文本框中输入的内容
var url = 'servlet/CheckUserNameServlet';
$.post(
url, //处理请求的url
'name':content, //传递的参数
function(data) //请求处理完毕之后的回调函数
if(data == 'true')//返回的是字符串不能用data = false这样比较
$("#s1").text("用户名不唯一");```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
else if(data == 'false')
$("#s1").text("用户名可用");
);
</script> </head>
<body>
<% request.setCharacterEncoding("utf-8");
%>
<form action="">
用户名:<input type="text" name="uerName" onblur="checkUnique();" id = "userName"/> <span id="s1"></span><br/>
密码:<input type="password" name="password" /><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
String userName = request.getParameter("userName");
if(userName!=null && !userName.isEmpty())
UserDao dao = new UserDao();
boolean b = dao.checkUserName(userName);
PrintWriter out = response.getWriter();
out.print(b);
out.flush();
out.close();
注意把jquery的jar报引到项目下,使用jquery方式实现ajax。 参考技术C 你这不是不会。是一点都不会?
以上是关于如何用Java实现Web服务器的主要内容,如果未能解决你的问题,请参考以下文章