在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

使用Get方式提交数据到Tomcat服务器的方法

2016-7-29 15:37| 发布者: zhangjf| 查看: 633| 评论: 0

摘要: 这篇文章将介绍向服务器发送数据,并且服务器将数据的处理结果返回给客户端,这次先介绍使用Get方式向服务器发送数据,下篇将介绍使用Post方式向服务器发送数据,需要的朋友参考下吧! 实现方式分为以下几步: 第一 ...

这篇文章将介绍向服务器发送数据,并且服务器将数据的处理结果返回给客户端,这次先介绍使用Get方式向服务器发送数据,下篇将介绍使用Post方式向服务器发送数据,需要的朋友参考下吧!

实现方式分为以下几步:

第一步:使用MyEclipse创建一个Web project,项目命名为WebProject->在src文件夹中新建一个包名为com.fyt.org的包
->在包中新建一个Servlet,Servlet命名为LoginServlet,并在LoginServlet.Java中添加下面的代码

  1. package com.fyt.org;
  2. import java.io.IOException;
  3. import java.io.OutputStream;
  4. import java.io.PrintWriter;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. public class LoginServlet extends HttpServlet {
  10. public LoginServlet() {
  11. super();
  12. }
  13. public void destroy() {
  14. super.destroy();
  15. }
  16. //使用Get方式向服务器提交数据
  17. public void doGet(HttpServletRequest request, HttpServletResponse response)
  18. throws ServletException, IOException {
  19. //获取从浏览器中发送过来的用户名
  20. String username = request.getParameter("username");
  21. //获取从客户端发送过来的密码
  22. String password = request.getParameter("password");
  23. //使用iso8859-1编码将username转换成字节数组
  24. //再使用utf-8把字节数组转换成字符串
  25. username = new String(username.getBytes("iso8859-1"), "utf-8");
  26. //在控制台中打印用户名和密码
  27. System.out.println("username=" + username);
  28. System.out.println("password=" + password);
  29. //获得一个输出流
  30. OutputStream os = response.getOutputStream();
  31. //如果用户名和密码都输入正确
  32. if("小志".equals(username) && "123".equals(password)) {
  33. //将字符发送至浏览器中
  34. os.write("登录成功".getBytes("utf-8"));
  35. }
  36. else {
  37. //将字符串发送到浏览器中
  38. os.write("登录失败".getBytes("utf-8"));
  39. }
  40. }
  41. //使用Post方式向服务器提交数据
  42. public void doPost(HttpServletRequest request, HttpServletResponse response)
  43. throws ServletException, IOException {
  44. }
  45. }
复制代码

第二步:启动Tomcat服务器,Tomcat服务器的启动方式可以参考我的博客在MyEclipse上部署Tomcat服务器

第三步:修改WebProject项目中的WebRoot目录下的index.jsp中的代码,index.jsp中的代码如下

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'index.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <!--
  16. <link rel="stylesheet" type="text/css" href="styles.css">
  17. -->
  18. </head>
  19. <body>
  20. <form action="servlet/LoginServlet" method="get">
  21. 用户名:<input type="text" name="username"><br>
  22. 密码:<input type="password" name="password"><br>
  23. <input type="submit" value="提交">
  24. </form>
  25. </body>
  26. </html>
复制代码

第四步:将项目部署到Tomcat服务器上,部署方式如下

1、点击下图中圈出的图标


2、Project中选择WebProject,并且单击Add按钮


3、Server中选择Tomcat 7.x,并且单击finish按钮


4、单击OK按钮,此时WebProject项目已经成功的部署到了Tomcat服务器上


第五步:打开浏览器,在浏览器中输入http://192.168.1.102:8080/WebProject/index.jsp,在浏览器中显示了下图所示的界面表示成功的访问到了服务器中的数据


在用户名中输入小志,在密码中输入123,单击登录按钮后,弹出了登录成功界面后表示登录成功了,因为设置的正确的用户名是小志,正确的密码是123


当在用户名和密码中输入错误的密码后,会提示登录失败

关于使用Get方式提交数据到Tomcat服务器的方法,小编就给大家介绍这么多,希望对大家有所帮助!

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-6 09:02

Copyright 2015-2025 djqfx

返回顶部