jsp 放在一个文件夹里,要响应Servlet的话action路径怎么写?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jsp 放在一个文件夹里,要响应Servlet的话action路径怎么写?相关的知识,希望对你有一定的参考价值。

Login.jsp文件在WebContent/User/下,LoginServlet.java在src/com.servlet下Login.jsp要响应LoginServlet,在action中写“LoginServlet”不行,跳转之后的路径变成.../项目名/User/LoginServlet 多了User这个文件夹,我知道是因为jsp在User文件夹下,把jsp直接放在WebContent下就可以。但我把action改为http..../项目名/LoginServlet或者../LoginServlet还是不行。
求教!

听得不大懂,给你看看我之前用的一个servletweb.xml内容,

这是目录结构,

参考技术A action那里不是要路径吗?只要使用URL映射tocamt服务器中webapps中对应的工程,在再工程内的WEB-INF目录中编写web.xml文件,servlet对应映射就是在web.xml控制的追问

你好!
新版的Servlet不在web.xml里配置了,在LoginServlet.java里的这句话@WebServlet("/LoginServlet")就是配置。
但我直接在action里写action="LoginServlet"不对,路径变成http../项目名/User/LoginServlet

参考技术B 将之前的@WebServlet("LoginServlet")改成@WebServlet("/User/LoginServlet"),然后form表单里的action写成action=“LoginServlet”,就OK啦

Session案例:简易的购物车

三个jsp和两个Servlet组成:在WebContent下边建立一个shoppingcart文件夹,将三个jsp文件放在里面:

 

1.建立一个step1.jsp文件,出现一个表格,是一个复选框,可以选择要购买的书籍,完毕后,点击Submit,跳转到Servlet类 ProcessStep1Servlet里面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h2>step1: 选择要购买的书籍!</h2>
    
    <form action="<%= request.getContextPath() %>/processStep1Servlet" method="post">
    
        <table border="1" cellpadding="10" cellspacing="0">
            <tr>
                <th>书名</th>  
                <th>点击购买</th>
            </tr>    
            
            <tr>
                <td>Oracle</td>  
                <td><input type="checkbox" name="book" value="Oracle"/></td>
            </tr>
            
            <tr>
                <td>java</td>  
                <td><input type="checkbox" name="book" value="java"/></td>
            </tr>
            
            <tr>
                <td>C++</td>  
                <td><input type="checkbox" name="book" value="C++"/></td>
            </tr>
            
            <tr>
                <td>mysql</td>  
                <td><input type="checkbox" name="book" value="mysql"/></td>
            </tr>
            
            <tr>
                <td>WEB</td>  
                <td><input type="checkbox" name="book" value="WEB"/></td>
            </tr>
            
            <tr>
                <td colspan="2"> <input type="submit" value="Submit"/></td>
            </tr>
            
        </table>
    
    </form>
</body>
</html>

 

2.建立一个Servlet类 ProcessStep1Servlet,里面有doPost方法,可实现session方法(好处就是获取用户选择书名的id,保存到后边再使用),获取用户选择的书名,使用相对路径的方法重定向到step1.jsp页面; response.sendRedirect(request.getContextPath()+"/shoppingcart/step2.jsp");

package com.lanqiao.javatest;

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

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

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        //System.out.println("jflskdflasdk");
        String [] books=request.getParameterValues("book");
        
        request.getSession().setAttribute("books", books);
        //System.out.println(request.getContextPath() + "/shoppingcart/step2.jsp");
        response.sendRedirect(request.getContextPath()+"/shoppingcart/step2.jsp");
    }

}

 

3.在step2.jsp页面,是用户输入个人信息页面,付款,发送货物需要的信息,然后跳转到Servlet类 processStep2Servlet 对用户的信息封装到customer类里面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h2>step2: 请输入寄送地址和信用卡</h2>
    
    <form action="<%= request.getContextPath() %>/processStep2Servlet" method="post">
        <table border="1" cellpadding="10" cellspacing="0">
            <tr>
                <td colspan="2">寄送信息:</td>
            </tr>
            
            <tr>
                <td>姓名:</td>
                <td><input type="text" name="name"/></td>
            </tr>
            
            <tr>
                <td>地址:</td>
                <td><input type="text" name="address"/></td>
            </tr>
            
            <tr>
                <td>联系电话:</td>
                <td><input type="text" name="phone"/></td>
            </tr>
        
            <tr >
                <td colspan="2">信用卡信息:</td>
            </tr>
            
            <tr>
                <td>信用卡种类</td>
                <td>
                    <input type="radio" name="cardType" value="nong"/>nong
                    <input type="radio" name="cardType" value="gong"/>gong
                </td>
                
            </tr>
            
            <tr>
                <td>信用卡号:</td>
                <td><input type="text" name="card"/></td>
            </tr>
            
            <tr>
                <td colspan="2"> <input type="submit" value="Submit"/></td>
            </tr>
        </table>
    
    </form>
</body>
</html>

 

4.建立一个Servlet类,里面有doPost方法,对用户的信息封装到customer类里面,session实例,保存用户的id信息,便于后边输出,然后重定向到confirm.jsp页面 ,跳转一面使用了相对路径:response.sendRedirect(request.getContextPath()+"/shoppingcart/confirm.jsp");

package com.lanqiao.javatest;

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

public class ProcessStep2Servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name=request.getParameter("name");
        String address=request.getParameter("address");
        String phone=request.getParameter("phone");
        String cardType=request.getParameter("cardType");
        String card=request.getParameter("card");
        
        Customer customer=new Customer(name,address,phone,cardType,card);
        //System.out.println("jiajai");
        HttpSession session=request.getSession();
        session.setAttribute("customer", customer);
        response.sendRedirect(request.getContextPath()+"/shoppingcart/confirm.jsp");
    }

}

 

5.confirm.jsp页面,使用了session实例,将前边的用户选择的书籍和用户输入的个人信息,输出到这个页面

<%@page import="com.lanqiao.javatest.Customer"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        Customer customer=(Customer)session.getAttribute("customer");
        String [] books=(String [])session.getAttribute("books");
    %>
     
     <h2>信息表单</h2>
    <table>
        <tr>
            <td>顾客姓名:</td>
            <td><%=customer.getName() %></td>
        </tr>
        
        <tr>
            <td>顾客地址:</td>
            <td><%=customer.getAddress() %></td>
        </tr>
        
        <tr>
            <td>顾客联系电话:</td>
            <td><%=customer.getPhone() %></td>
        </tr>
        
        <tr>
            <td>顾客卡类型:</td>
            <td><%=customer.getCardType() %></td>
        </tr>
        
        <tr>
            <td>顾客的卡号:</td>
            <td><%=customer.getCard() %></td>
        </tr>
        
        <tr>
            <td>您购买的书:</td>
            <td>
                <%
                    for(String book:books){
                        out.print(book);
                        out.print("\t\t");
                    }
                %>
            </td>
        
        </tr>
    
    </table>
</body>
</html>

 

6.lib下边的web.xml文件

<?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_2_5.xsd" id="WebApp_ID" version="2.5">
  <servlet>
    <description></description>
    <display-name>ProcessStep1Servlet</display-name>
    <servlet-name>ProcessStep1Servlet</servlet-name>
    <servlet-class>com.lanqiao.javatest.ProcessStep1Servlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ProcessStep1Servlet</servlet-name>
    <url-pattern>/processStep1Servlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>ProcessStep2Servlet</display-name>
    <servlet-name>ProcessStep2Servlet</servlet-name>
    <servlet-class>com.lanqiao.javatest.ProcessStep2Servlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ProcessStep2Servlet</servlet-name>
    <url-pattern>/processStep2Servlet</url-pattern>
  </servlet-mapping>
</web-app>

 

以上是关于jsp 放在一个文件夹里,要响应Servlet的话action路径怎么写?的主要内容,如果未能解决你的问题,请参考以下文章

4.在Java Web应用中,JSP文件应该放在啥地方,编译后的Servlet和JavaBean应该放在啥地方?

jsp button提交一个值到servlet

servlet中request.getrequestDispatcher()的路径问题

java jsp 一般把图片上传后要保存在哪里?如果是放在部署的文件下那麼下次启动就没了。

frame 标记 引用一个jsp文件应该怎么写??

jsp+servlet怎样防止用户用URL直接访问页面