Servlet实例一

Servlet实例流程:

login.jsp-LoginServlet-打印登陆信息


第一步:新建LoginServlet.java文件


package com.jikexueyuan.servlet;


import java.io.IOException;


import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


public classLoginServlet extendsHttpServlet {


//点击 public class LoginServlet extends HttpServlet左边的警告 按钮  点击Add generated serial version ID 生成以下一行代码:序列号版本ID,用于序列化和反序列化

/**

*/

privatestatic finallong serialVersionUID =6520550639750164887L;


@Override

protectedvoid service(HttpServletRequestreq, HttpServletResponse resp)throws ServletException,IOException {

         String userName= req.getParameter("uname");

         String password= req.getParameter("upwd");

         

         System.out.println("用户名 ==》 "+ userName);

         System.out.println("密码 ==》 "+ password);

}

}



第二步: WebContent 文件夹下新建14文件夹:新建login.jsp文件

<%@ pagelanguage="java"contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE htmlPUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type"content="text/html; charset=UTF-8">

<title>登录界面</title>

</head>

<body>

 

  <!-- request.getContextPath() 应用的上下文路径http://localhost:8080/JSPStudy -->

  <form action="<%= request.getContextPath()%>/loginServlet"method="post">

   userName:<input type="text"name="uname" /> <br/>

   password:<input type="password"name="upwd" /> <br/>

   <input type="submit"name="Login" />

   <input type="reset"name="Reset" />

  </form>

</body>

</html>



第三步:打开WEB-INF下的web.xml添加以下代码

  <servlet>

      <servlet-name>LoginServlet</servlet-name>

      <servlet-class>com.jikexueyuan.servlet.LoginServlet</servlet-class>

  </servlet>

  

  <servlet-mapping>

      <servlet-name>LoginServlet</servlet-name>

      <url-pattern>/loginServlet</url-pattern>

  </servlet-mapping>


第四步:启动Tomcat

第五步:打开浏览器 http://localhost:8080/JSPStudy/14/login.jsp 

输入用户名developer和密码111111 点击“提交”按钮

eclipse控制台会输出形如

用户名 ==》 developer

密码 ==》 111111



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值