servlet

Servlet是利用Java类编写的服务端应用程序,它可以被看作是位于客户端和服务器的一个中间层,负责接受和请求客户端用户的响应。
1:对客户端发送的数据惊醒读取和拦截
2:读取客户端请求的隐含数据
3:运行结果或者生活结果
4:发送响应的数据

Servlet的技术特点
1:高效率
2:简单方便

Servlet的生命周期

// 初始化
public void init() throws ServletException {
	super.init();
}

// 运行
protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
		throws ServletException, IOException {
	super.service(arg0, arg1);
}

// 销毁
public void destroy() {
	super.destroy();
}

Servlet的xml文件配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
  <servlet-name>login<rvlet-name>
  <servlet-class>org.cgq.servlet.LoginServlet<rvlet-class>
  <rvlet>
  
  <servlet-mapping>
  <servlet-name>login<rvlet-name>
  <url-pattern>login</url-pattern>
  <rvlet-mapping>
</web-app>

index文件

  <body>
   <form action="test" method="post">
   用户名:<input type="text" name="name1"/><br/>
   密码:<input type="password" name="pwd1"><br/>
   <input type="submit" value="登录">
   </form>
  </body>

LogIn文件

package org.sunjinglan.test;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Servlet extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String name= req.getParameter("name1");
		String pwd=req.getParameter("pwd1");
		System.out.println("姓名:"+name+"密码:"+pwd);
		System.out.println("get...");	
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String name= req.getParameter("name1");
		String pwd=req.getParameter("pwd1");
		System.out.println("姓名:"+name+"密码:"+pwd);
		System.out.println("post...");
	}
	
}

最后在浏览器上运行就好

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值