前端发送post请求,如何跟后端交互http://www.cnblogs.com/huhuuu/p/3993019.html
来源:互联网 发布:淘宝双11什么时候开始 编辑:程序博客网 时间:2024/05/09 13:56
前端发送post请求,如何跟后端交互
Posted on 2014-09-25 16:07 huhuuu 阅读(2619) 评论(0) 编辑 收藏假如,现在我做了一个给用户使用的html页面:
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title></head><h1>helloPost!</h1><body><form method="post" action="SelectBeer.do"> <p>Select beer</p> <select name="color" size="1"> <option value="light"> light</option> <option value="amber"> amber</option> <option value="brown"> brown</option> <option value="dark"> dark</option> </select> <center> <input type="submit"> </center></form>></body></html>
<form method="post" action="SelectBeer.do"> 中的SelectBeer.do有点像资源名。最终会被http:/127.0.0.1:8080/文件名/SelectBeer.do 的形式访问到。这是我又写了后台servlet程序,实现了get,post的方法:
package com.example.web;import java.io.IOException;import java.io.PrintWriter;import java.sql.Time;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class HelloWorld2 */@WebServlet("/HelloWorld2")public class HelloWorld2 extends HttpServlet { private static final long serialVersionUID = 1L; /** * Default constructor. */ public HelloWorld2() { // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ @SuppressWarnings("deprecation") protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub PrintWriter out = response.getWriter(); //out.print("helloWorld"); //String outPut=request.getParameter("input"); out.print(request); System.out.println(request); String c = request.getParameter("color"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("Beer Selection Advice<br>"); String c = request.getParameter("color"); out.println("<br> Got beer color"+c); System.out.println(c); }}
把html放到servlet目录下,发现前后端无法通信。
这时还需要在web.xml下进行设置。
<servlet> <servlet-name>123123</servlet-name> <servlet-class>com.example.web.HelloWorld2</servlet-class> </servlet> <servlet-mapping> <servlet-name>123123</servlet-name> <url-pattern>/SelectBeer.do</url-pattern> </servlet-mapping>
servlet:可以自己设置servlet的资源名字, 然后写上对应的servlet类名
servlet-mapping:自己的servlet与资源的映射关系发送Post请求:
返回:
发送get请求:参考:head first,servlet & jsp
0 0
- 前端发送post请求,如何跟后端交互http://www.cnblogs.com/huhuuu/p/3993019.html
- PHP 发送邮件配置 转发自http://www.cnblogs.com/dee0912/p/5448350.html
- SpringMVC处理请求流程(http://www.cnblogs.com/hujiapeng/p/5765636.html)
- http://www.cnblogs.com/flylovesky/p/3274869.html
- http://www.cnblogs.com/ada-zheng/p/3284660.html
- http://www.cnblogs.com/zhanghaiba/p/3548602.html
- http://www.cnblogs.com/scy251147/p/3566638.html
- http://www.cnblogs.com/amosli/p/3577645.html
- http://www.cnblogs.com/VisualImage/p/3594973.html
- http://www.cnblogs.com/kuangbin/p/3164106.html
- http://www.cnblogs.com/wengzilin/p/3530712.html
- http://www.cnblogs.com/qingjoin/p/3549325.html
- http://www.cnblogs.com/interdrp/p/3785164.html
- http://www.cnblogs.com/ruiati/p/3930732.html
- http://www.cnblogs.com/knowledgesea/p/3491214.html
- http://www.cnblogs.com/ganganloveu/p/3755191.html
- http://www.cnblogs.com/kenshincui/p/3885689.html
- http://www.cnblogs.com/lanxuezaipiao/p/3440471.html
- 设计模式:9)装饰者模式
- iOS库.a与.framework
- leetcode 214:Shortest Palindrome 题目分析 与使用KMP算法的java实现
- CVE-2015-4852 java 反序列化漏洞--weblogic补丁
- STL中map 和 hash_map 的使用方法
- 前端发送post请求,如何跟后端交互http://www.cnblogs.com/huhuuu/p/3993019.html
- getContextPath、getServletPath、getRequestURI的区别
- hadoop和hive压缩格式对比【数据源建议RCFile+bz或RCFile+gz的方式可以节省空间,计算过程建议RCFile+snappy,空间换时间】
- Android Support Design Library之NavigationView
- TCP打洞和UDP打洞的区别
- 两条未知曲线,求其最短距离
- Color the fence(南阳oj791)(贪心+策略)
- 4231412
- Web Service 、SOAP 、WSDL知识介绍