【问题标题】:How to navigate to another html file using apache tomcat servlets and send jinja variables?如何使用 apache tomcat servlet 导航到另一个 html 文件并发送 jinja 变量?
【发布时间】:2021-08-19 14:07:25
【问题描述】:

使用 flask python 来渲染另一个模板并在获取请求时发送 jinja 变量,您可以简单地执行以下操作:

@app.route("/login", methods=["GET", "POST"])
def login():
    # User reached route via POST (as by submitting a form via POST)
    if request.method == "POST":
        # Ensure username was submitted
        username = request.form.get("username"):
        password = request.form.get("password"):
        # Redirect user to home page
        return render_template("home.html", username=username)

    # User reached route via GET
    else:
        return render_template("login.html")

在 Java 上使用 tomcat Servlets 的等价物是什么?

【问题讨论】:

  • 在您的 python 代码中,您实际上并没有重定向。您只是在渲染“主页”页面上的内容,但您的用户仍在您的登录路径上。

标签: java html tomcat servlets


【解决方案1】:

使用 Servlet 需要两部分:配置文件 (web.xml) 和 Servlet 代码。

  1. 在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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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>Servlet Example</display-name>
  <welcome-file-list>
    <!-- you will want to take a look at jsp as well --> 
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>Login Servlet</servlet-name>
    <!-- dont forget to put the canonical name, including the package name -->
    <servlet-class>com.example.LoginServlet</servlet-class>
  </servlet>
</web-app>
  1. 然后你编写 servlet:

    package com.example;
    public class LoginServlet extends HttpServlet {
    
    // GET
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.sendRedirect("index.jsp");
    }
    
    // POST
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
    
        // method credentialsOk validate the user/password
        if(credentialsOk(username, password)){
            response.sendRedirect("home.jsp");
        }else{
            response.sendRedirect("login.jsp");
        }
     } 
    

    }

看这里的 Servlet 介绍:https://www.baeldung.com/intro-to-servlets

【讨论】:

  • 这看起来不错,但我仍然不明白如何将 jinja 变量发送到 html 文件
  • @AmmarMohamed render_template 函数获取参数并传递给模板引擎。在您的示例中,当使用 POST 时,您传递了“用户名 = 用户名”,它应该在您的 jinja 模板代码中有一个 {{ username }},以便 jinja 引擎模板将使用 render_template 用户名变量。
  • 在 Java 中类似,但不是使用 jinja 模板,而是使用 Jsp 页面。在这里看看它是如何工作的:baeldung.com/jsp
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-13
  • 2016-09-23
  • 1970-01-01
相关资源
最近更新 更多