访问到Servlet 及URL各部分含义

    xiaoxiao2023-10-06  174

    例如: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方法 }

    在浏览器中拼接了一个理想的访问请求

    最新回复(0)