gpt4 book ai didi

java - Jsp页面关闭html和body标签的目的

转载 作者:行者123 更新时间:2023-11-29 04:28:46 25 4
gpt4 key购买 nike

我有一个关于 Jsp 和 HTML 的非常基本的问题,下面的例子来自 Java EE7 黑皮书。

登录.html

<html> 
<body>
<pre>
<form action="LoginProcess.jsp">
<b>User Name</b> : <input type="text" name="uname"/>
<b>Password</b> : <input type="password" name="pass"/>
<input type="submit" value="LogIN"/>
</form>
</pre>
</body>
</html>

下面是JSp的代码

<%@page import="java.sql.*" errorPage="\MyError.jsp"%>
<html>
<body>
<%
Connection con=null;
String uname=request.getParameter("uname");
String pass=request.getParameter("pass");
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.123:1521:XE","scott","tiger");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from userdetails where uname='"+uname+ "' and pass='"+ pass +"'");
if (!rs.next())
{

%>

User details given for user name : <%=request.getParameter("uname")%> and
password : <%=request.getParameter("pass")%> are not valid <br/> Try again
<%@include file="Login.html"%>
</body>
</html>
<%
return;
}//if
}//try
finally
{
try
{
con.close();
}catch(Exception e){}
}//finally
%>

This is a Home Page <br/>
Welcome, <%=uname%>

我的问题是关于 </body> & </html>卡在 return 语句正上方代码中间的标签 [不知道如何在代码中突出显示它们]。我不明白这两个标签的用途。我对为什么关闭 html 和 body 标签感到困惑。以及如何在关闭 html 标签后呈现更多代码。有人可以对此有所了解吗。

提前致谢。

最佳答案

所以,虽然它是非常古老的风格代码,但让我对它进行一些说明(也为了其他初学者):

首先,您提供的代码非常丑陋和陈旧。

作为学术观点-

在一个jsp中,你可以有3种类型的基本标签-

  1. 小脚本: <% %>
  2. 声明: <%! %>
  3. 表达式: <%= %>

所有 jsp 页面都在内部转换为等效的 servlet,并且在等效的 servlet 文件中为每个 jsp 创建默认方法 _jspService(httpServletRequest, httpServletResponse)。

放置在 scriptlet 中的代码自动进入 _jspService(httpServletRequest, httpServletResponse)

因此:

  • 在 scriptlet 中声明的变量成为 _jspService(httpServletRequest, httpServletResponse) 方法的局部变量。
  • 因为 java 不支持方法嵌套,我们不能在 scriptlet 中放置任何方法。
  • 在声明标签中,您可以声明变量。
  • 放置在 jsp 声明标签内的代码位于_jspService(httpServletRequest, httpServletResponse) 等效servlet的方法
  • 在声明标签内声明的变量成为实例等效 servlet 类的变量。
  • 在 jsp 中我们有 9 种类型的隐式对象(请求、响应、输出、页等)
  • 并且所有隐式对象都是_jspService(req, res)的局部变量方法,因此这些隐式对象在内部不可访问jsp的声明标签。
  • 表达式标签评估给定的表达式并写入生成到浏览器窗口的输出作为网页内容。
  • 算术运算、逻辑运算、java方法调用返回值等可以放在表达式标签内。

除了这3个scripting标签,我们在jsp中还有3个directive标签

  1. Page Directive 标签: <%@page %> 该标签向 jsp 等效 servlet 程序提供全局信息,如导入包、增加缓冲区大小、指定错误页面等。
  2. Include Directive Tag: <%@include file="... "%> 这个标签包括目标 jsp 的等效 servlet 代码到源 jsp 的等效 servlet
  3. Taglib 指令: <%@taglib %> 用于在我们的 jsp 文件中包含和使用 JSTL 标签库。

除此之外,我们还有 8 个以上的标准 Action 标签:

 1. <jsp:useBean >
2. <jsp:forward > etc.

现在回答您的问题:您对为什么关闭 html 和 body 标签感到困惑。以及如何在关闭 html 标签后呈现更多代码。

首先,我在上面告诉过你的是非常老式的编码,完全不合时宜。我们总是建议使用 JSTL 和其他jsp 中的标签,以保持 jsp 文件易于阅读、清洁和易于维护。 JSP 文件用于 View 层,我们不应该在jsp 文件中放置任何java 代码。所以你给的jsp文件一团糟。

在 body 和 html 标签结束后编写的代码是一个 scriptlet,它将在 _jspService(req, rs) 方法内转换为等效的 java 代码,该方法用于 jsp 等效的 servlet 类。

   <% 
return;
}//if
}//try
finally
{
try
{
con.close();
}catch(Exception e){}
}//finally
%>

这段代码是简单的文本和一个表达式标签(它再次为您提供了一个以文本格式显示在网页上的输出)

This is a Home Page <br/>
Welcome, <%=uname%>

[> Omitting the html, head, and body tags and writing text outside them is certainly allowed by the HTML specs. The underlying reason is that browsers have always sought to be consistent with existing web pages, and the very early versions of HTML didn't define those elements. When HTML 2.0 first did, it was done in a way that the tags would be inferred when missing.][source]1

有关详细信息,请参阅 here

关于java - Jsp页面关闭html和body标签的目的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44787258/

25 4 0