您的位置:首页 > 编程语言

servlet实现控制台输出用户名和密码的相关步骤和代码

2016-06-24 15:51 225 查看
1、创建一个servlet,命名ServletDemo1,在浏览中访问该servlet
代码如下:

package 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 ServletDemo1 extends HttpServlet {

 /**

  * Constructor of the object.

  */

 public ServletDemo1() {

  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 {

  String mes=request.getParameter("mes");

  System.out.print(mes);

 }

 

 public void doPost(HttpServletRequest request, HttpServletResponse response)

   throws ServletException, IOException {

this.doGet(request, response);

 }

 /**

  * Initialization of the servlet. <br>

 

  * @throws ServletException if an error occurs

  */

 public void init() throws ServletException {

  // Put your code here

 }

}

2、创建一个带超链接的jsp页面,超链接到ServletDemo2,在控制台输出jsp页面通过超链接参数传递的用户名和密码。

Index.jsp 代码参考

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%

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>

   <a href="/ZY/ServletDemo2?name=zy&&pass=123">hahhahahhahaha</a>

  </body>

</html>

</html>

SevletDemo2.java 代码参考

package 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 ServletDemo2 extends HttpServlet {

 /**

  * Constructor of the object.

  */

 public ServletDemo2() {

  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 {

  String name=request.getParameter("name");

  String pass=request.getParameter("pass");

  System.out.println("用户名:"+name);

  System.out.println("密码:"+pass);

 }

 

 public void doPost(HttpServletRequest request, HttpServletResponse response)

   throws ServletException, IOException {

this.doGet(request, response);

 }

 /**

  * Initialization of the servlet. <br>

  *

  * @throws ServletException if an error occurs

  */

 public void init() throws ServletException {

  // Put your code here

 }

}

3、创建一个用户登录的jsp页面,用户在表单中输入用户名和密码,提交后跳转到ServletDemo3,在控制台输出用户名和密码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  servlet 控制台