我来自.NET的背景,刚开始使用Java。我发现Java SE没有问题,因为它们严格遵循相同的概念。但是,在使用Java EE时,我发现它比ASP .NET复杂一点。
我正在尝试在Java EE应用程序中实现N层架构。所以我已经写了我的逻辑和放大器数据层并在Java SE应用程序(这是我的管理后端应用程序)中成功测试它们,并且刚刚开始处理我的显示层。
问题就出现了。我试图从JSP调用我的业务逻辑层(由按钮事件触发)来调用某个方法并将结果输出(例如boolean)返回给JSP。
我该怎么做?
答案 0 :(得分:2)
即使没有你,你也可以使用 Servlet 执行简单的反手操作 或者你可以使用 Java Beans 这是一个简单的业务逻辑实现 请参阅java bean的示例,请{h} {/ 3}}
Servlet 的示例在下面给出
<强>的index.jsp 强>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="add" method="post">
Value 1:<input type="text" name="val1" id="val1"/><br>
Value 2:<input type="text" name="val2" id="val2"/><br>
<input type="submit" value="Submit"/><br>
</form>
<%String sum="";
sum = (String)request.getAttribute("val3"); %>
<input type="text" value="<%=sum%>" />
</body>
</html>
<强>的web.xml 强>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>add</servlet-name>
<servlet-class>controller.add</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>add</servlet-name>
<url-pattern>/add</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<强> add.java 强>
package controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class add extends HttpServlet {
String val3;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String val1=request.getParameter("val1");
String val2=request.getParameter("val2");
if(val1 != null && val2 != null)
val3=""+(Integer.parseInt(val1)+Integer.parseInt(val2));
else
val3="";
request.setAttribute("val3",val3);
request.getRequestDispatcher("index.jsp").forward(request, response);
try {
} finally {
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
答案 1 :(得分:0)
在ASP.NET中,这种东西被.Net(javascripts和其他东西插入到.Net中的页面)所取代,但在java中,你必须自己编写它们。在.Net中你有一些像do_postback
这样的javascript方法但在java中你应该写javascripts。看到这段代码:
脚本:
<script>
function doPostback() {
// calling some java method using Ajax or Http Post method or redirect to another page
}
</script>
HTML:
<button type="button" onclick="doPostback()">Go</button>