码迷,mamicode.com
首页 > 编程语言 > 详细

Python与JavaWeb的第一次碰撞

时间:2019-01-26 15:23:22      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:version   imp   学习   webapp   使用   ...   charset   static   mon   

在Python中向服务器提交一个表单数据看起来是很容易的,但是这次经历着实让我记忆深刻,借此也为了警醒同样遇到了这样问题的你们。


要做什么?

使用Python的urllib2模块提交表单数据,并在服务器端进行验证提交的表单结果。

  • 操作系统
    Windows 7 旗舰版

  • 需要的编译器:

    • Eclipse
    • PyCharm
  • 需要的技术:
    • (基础的)Java web技术
    • (基础的)Python

服务器端代码

服务器端采用JavaWeb技术创建,使用Servlet来接收表单数据。具体内容如下:

首先是index.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="GetParameters" method="POST">
    <br>
    UserName : <input type="text" name= "username"><br>
    Password : <input type="password" name = "password"><br>
    <br>
    <input type= "submit" value="Submit">
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="reset">

</form>

</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

然后是action对应的servlet界面。GetParameters.java

package one;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GetParameters extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetParameters() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
        doPost(request, response);

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        // doGet(request, response);
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        System.out.println("Username is : " + username);
        System.out.println("Password is : " + password);

        response.getOutputStream().write("I have received you request!".getBytes());
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

最后就是web.xml的配置文件(我这里偷了个懒,老是记错配置,所以就在tomcat的一个sample 下面抄了一个,然后改了改,就成了)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>Test</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>GetParameters</servlet-name>
        <servlet-class>one.GetParameters</servlet-class>
    </servlet>
    <!-- ... -->
    <servlet-mapping>
        <servlet-name>GetParameters</servlet-name>
        <url-pattern>/GetParameters</url-pattern>
    </servlet-mapping>

</web-app>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

Python端代码

额,我姑且称之谓测试端吧。代码很简单,如下:

# coding:UTF-8
import sys,urllib,urllib2

def CommonWay():
    url = ‘http://localhost:8080/Test/GetParameters‘
    params = {
        ‘username‘:‘Username‘,
        ‘password‘:‘Password‘
    }
    data = urllib.urlencode(params)
    req = urllib2.Request(url,data)
    response = urllib2.urlopen(req)
    content = response.read()
    print content

CommonWay()
print "Succeed!"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

运行结果

Python控制台的输出结果是这样的

D:\Software\Python2\python.exe E:/Code/Python/GetWeather/ulib2/SubmitData.py
I have received you request!
Succeed!

Process finished with exit code 0
  • 1
  • 2
  • 3
  • 4
  • 5

但其实最重要的就是服务器端的处理结果了。
技术分享图片

我们可以在Eclipse的控制台清楚的看到我们提交的表单数据。至于要对这些数据做什么样的操作,就不是今天要讲的内容了。

总结

  • 今天的收获很多,作为一个学习Python的新手,我坚信现阶段遇到的问题越多,进步的也就会越快。
  • 对一个知识点要学的深入一点,而不只是停留在表面
  • 少犯一些低级错误,比如我今天这个URL竟然都给弄错了。⊙﹏⊙b汗
  • 多多的写代码,多多的测试,才能比较扎实的掌握。而不要图快,这样基础根本打不牢固。

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

Python与JavaWeb的第一次碰撞

标签:version   imp   学习   webapp   使用   ...   charset   static   mon   

原文地址:https://www.cnblogs.com/djuwcnhwbx/p/10323389.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!