访问到Servlet 及URL各部分含义

本文介绍了Servlet的访问路径,如http://localhost:8080/APP/loginaction,解释了各部分含义,包括超文本传输协议、服务器地址及端口、项目名称和URL映射。还指出在Web项目中常出现404错误,大多是web.xml文件配置问题,并给出代码示例。

例如:http://localhost:8080/APP/loginaction
http:// 超文本传输协议 客户端浏览器或者其他的web应用层传输协议,是一种明文文传输方式,了解具体信息可以自行百度或者自行学习《计算机网络》第七版

localhost:8080 是你访问的服务程序所在主机的地址及开放端口,即服务器的IP地址和对应的端口地址。这里我的服务器就是自己的电脑,直接用浏览器访问,所以我用的就是 localhost(默认的 localhost 就是127.0.0.1,就是指本地);如果你经过网络(不管是局域网、或者是 Internet),就需要用真正的服务器 IP 地址了(命令行使用 ipconfig 命令即可查看电脑IP),注意 只有具有公网IP 的才能通过 Internet 访问到,否则只能通过局域网使用(后期使用时可以在电脑开热点,然后手机连接后访问自己电脑的服务器)

APP/loginaction
APP就是自己创建的java web 应用的项目名称,loginaction是url——mapping 中映射。
以上几个部分 才是真正的servlet 的访问路径。 在平时的web项目中经常性的出现 404错误,提示找不到一个文件怎么滴,大部分错误原因都是在web.xml文件配置有问题。

访问到servlet的过程

借用大佬的图  自己体会体会 慢慢的就懂了
在这里插入图片描述下面附带 自己写的一段代码示例::

//@WebServlet("/loginaction")
public class loginaction extends HttpServlet {
	private static final long serialVersionUID = 1L; 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public loginaction() {
        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
		String account = request.getParameter("account");//从resqust中获取关键字对应的字符 getparameter 方法
	    String password = request.getParameter("password");
	    System.out.println("account   "+account+"\npassword   "+password);
	    
	    String res;
	    if("abc".equals(account)&&"123".equals(password))//进行服务器登陆验证
	    {
	    	res="login success";
	    }
	    else {
	    	res ="login fail  case by:account or password error";
	    }
	    PrintWriter pw = response.getWriter();//获取response输出流
	    pw.append(res);//append方法  输出登陆结果
	    //pw.println(res);
	    pw.flush();
	    pw.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);//post方法  与get方法

		
	}

在这里插入图片描述
在浏览器中拼接了一个理想的访问请求

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值