myeclipse servlet的基本运用

1.servlet:以数据为基础,不是独立存在与服务器上的网页文件

2.c/s负荷轻,维护成本高,b/s的负荷重,维护成本低

3.servlet的init()在执行时已被初始化,只一次; destroy()在服务器正常被关闭时销毁

4.改变初始化时间  <load-on-startup>1</load-on-startup>

5.servlet从前端页面获取数据  String qq=request.getParameter("qq");

LoginSeverlet

package com.jkx.web.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.jkx.dao.UserDao;
import com.jkx.po.User;

public class LoginServlet 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 {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out =response.getWriter();
		String qq=request.getParameter("qq");
		String pass=request.getParameter("pass");
		UserDao userdao=UserDao.getIntance();
		User user=userdao.getUserByusernameAndPasswoed(qq, pass);
		if(user!=null)
			System.out.println("登录成功");
		else
			System.out.println("登录失败");
		
		out.flush();
		out.close();
		
	}
	
//	public void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
//	{
//		response.setContentType("text/html");
//		response.setCharacterEncoding("utf-8");
//		System.out.println(request.getMethod());//获得传递方式
//		System.out.println(request.getRequestURI());
//		System.out.println(request.getContextPath());
//		String qq=request.getParameter("qq");
//		String pass=request.getParameter("pass");
//		System.out.println(qq+"\n"+pass);
//		PrintWriter out =response.getWriter();
//		out.println("dddddd 阿萨德<font color='red'>1111</font>");
//		
//		out.flush();
//		out.close();
//	}

}
前端代码:
<%@ 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>
	<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">
	-->
	  <style type="text/css">
	*{padding:0;margin:0;font-family:"微软雅黑";font-size:14px;}
	#warp{width:900px;height:800px; margin:0 auto;}
	#yx{height:390px;width:340px;border:1px solid #0099ff;border-radius:6px;margin:80px auto;}
	#yx_h{width:340px;height:50px;border-bottom:1px solid #0099ff;background:#00cccc;}
	#yx_h .span1{display:block;height:50px;width:170px;float:left;line-height:50px;text-align:center;cursor:pointer;font-size:16px;}
	#yx_h .span1:hover{background:#339999;}
	#yx_h .span2:hover{background:#339999;}
	#yx_h .span2{display:block;height:50px;width:170px;float:right;line-height:50px;text-align:center;cursor:pointer;font-size:16px;}
	#yx_b{width:290px;height:262px;padding:24px;}
	#yx_b span{width:286px;height:36px;display:block;line-height:36px;}
	#yx_b span input{width:286px;height:36px;border:1px solid #0099ff;margin-top:20px;display:block;padding-left:10px;}
	#yx_b span .chbox{width:18px;height:18px;float:left;line-height:20px;}
	#yx_b span .fot{width:250px;height:36px;display:block;float:right;margin-top:12px;margin-left:10px;font-size:12px;}
	#yx_b span .sub{color:#fff;font-size:16px;background:#0099ff;cursor:pointer;border-radius:5px;}
	#yx_b span .sub:hover{background:#3399cc;}
	#yx_t{width:340px;height:50px;}
	#yx_t span{width:312px;height:20px;display:inline-block;text-align:right;padding-right:24px;color:#0099cc;}
	#yx_t span a{text-decoration:none;color:#0099cc;}
	#yx_t span a:hover{text-decoration:underline;}
  </style>
	
  </head>
  
  <body>
  <form action="LoginServlet" method="post">
    <div id="warp">
		<div id="yx">
			<div id="yx_h">
				<span class="span1">快速登录</span>
				<span class="span2">账号密码登录</span>
			</div>
			<div id="yx_b">
				<span><input type="text" placeholder="支持QQ号/邮箱/手机号登录" name="qq"/></span>
				<span><input type="password" placeholder="QQ密码" name="pass"/></span>
				<span><input type="checkbox" class="chbox"><span class="fot">下次自动登录</span></span>
				<span><input type="submit" class="sub" value="登     录"/></span>
			</div>
			<div id="yx_t">
				<span>
					<a href="javascript:;">忘记密码?</a>  |
					<a href="javascript:;">注册新账号</a> |
					<a href="javascript:;">意见反馈</a>
				</span>
			</div>
		</div>
	</div>
	</form>
  </body>
</html>



6.PrintWriter out =response.getWriter(); out.println("dddddd");//以文本的形式输出servlet中需要输出的内容
7.response.setContentType("text/html");//不加若有标签 则标签一起打印;response.setCharacterEncoding("utf-8");中文乱码问题
8.request.getMethod();//获得传递方式 getContextPath、getRequestURL分别是获得项目名和域名端口号以后的部分,不包含参数,getScheme()获取协议名,getRequestPath等

9.获取网络连接的信息 getRemoteAddr() 返回客户端的ip地址 getRemotePort()返回客户端发出请求的端口 等

10.创建servlet,在项目里面新建一个servlet文件→命名后→注意删除红圈部分


11.编写servlet代码

package com.jkx.web.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;

public class FirstServlet extends HttpServlet {


	public void destroy() {
		System.out.println("FirstServlet被销毁");
	}

	public void init() throws ServletException {
		System.out.println("FirstServlet被初始化");
	}
	
	public void service(HttpServletRequest request, HttpServletResponse response)
	{
		System.out.println("111111-------------------------");
	}
	

	

}

13.具体项目请参照 http://download.csdn.net/download/qq_28316183/9969872

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值