الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

كيف استخدم Class جافا داخل صفحة Jsp

بدأه hamzalion في 16 سبتمبر 2008 · 12 رد · 2,559 مشاهدة · في JavaEE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع
<%@ page session="true" %>
<%@ page import="java.util.LinkedList" %> 

<%
String id=request.getParameter("id");
String quantity=request.getParameter("quantity");

	   Integer oid=new Integer(0);
		  int idi=oid.parseInt(id); 

		 int quantityi=oid.parseInt(quantity);









 HttpSession session1 = request.getSession();
  LinkedList all =(LinkedList)session1.getAttribute("all");

  Integer count=(Integer)session1.getAttribute("co");  



 if(all == null)
 {
  all=new LinkedList(); 
  count=new Integer(0);

  Mychart mychart=new Mychart(idi,quantityi);
  all.add(count.intValue(),mychart);
  session.setAttribute("all", all);
  session.setAttribute("co", count);
 }

 else
 {


 Mychart mychart=new Mychart(idi,quantityi);
 int c=count.intValue();
 count=new Integer(c+1);
 all.add(count.intValue(),mychart);
%>
 <script language=javascript>
 alert("you add to chart successfully");
 </script>
<%
 int i=0; 
	while(all.size()>i)
  {
  Mychart a=(Mychart)all.get(i);

 out.println(a.getid());
 i++;
  }

  session.setAttribute("all", all);
  session.setAttribute("co", count);






 }


%>

<%!


 class Mychart 
 {
 public int id;
 public int quantity;

 public Mychart(int i,int q)
 {
 id=i;
 quantity=q;
 }

 public void setid(int id)
 {this.id=id;}

 public int getid(){return id;}
 public int getq(){return quantity;}

 public void setq(int q)
 {this.quantity=q;}


 }

%>

المشكلة الاولى :

الكلاس Mychart حسنت استخدموا في هذه الصفحة فقط .

بدي استخدموا بصفحة تانية كيف؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟

المشكلة التانية:

  package examples;

 public class ReloadedClass
  {
	 public static String getMessage()
	  {
		 return "This is the original message";
	  }
 }

  <%@ page language="java" import="examples.*" %>
  <html>
  <body>

  The message is: <%= ReloadedClass.getMessage() %>
  </body>
  </html>

الصفحة الاولى سميتها ReloadedClass.java

وانفذت الصفحة التانية بس يعطيني خطأ انو الحزمة example لايجدها

تم تعديل هذه المشاركة بواسطة hamzalion في 16 سبتمبر 2008 في 16:01

#2
اقتباس
المشكلة الاولى :

الكلاس Mychart حسنت استخدموا في هذه الصفحة فقط .

بدي استخدموا بصفحة تانية كيف؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟

انت ممكن تعمله منه Object و تحفظه فى Session بإستخدام setAttribute و تستخدمه فى الصقحة التانية باستخدام ال getAttribute بس الافضل انك تعرفها ك class منفصلة مش لازم فى نفس الصفحة مدام هتستخدمها فى صفحة تانية

تم تعديل هذه المشاركة بواسطة M o 3 t a z في 16 سبتمبر 2008 في 18:23

#3

شكرا على الرد

اقتباس
بس الافضل انك تعرفها ك Class منفصلة مش لازم فى نفس الصفحة مدام هتستخدمها فى صفحة تانية

طيب شو اعمل .

يعني شو الطريقة انو عرف class بالجافا وبعدين استخدم غرض منه في اي صفحة.

بعدين الطريقة التانية ليش ما اشتغلت.

package examples;
public Class ReloadedClass
  {
	 public static String getMessage()
	  {
		 return "This is the original message";
	  }
}

<%@ page language="java" import="examples.*" %>
  <html>
  <body>

  The message is: <%= ReloadedClass.getMessage() %>
  </body>
  </html>
#4

كيف استخدم class جافا داخل صفة jsp ?????????????????

#5
hamzalion كتب:
كيف استخدم class جافا داخل صفة jsp ?????????????????

السلام عليكم أخي ٌلنا ما تريده بالدات أو انشر الكود حتى تتم مساعدتك

image-27.gif

concepteur realisateur java j2ee

php5 and asp

#6

أخي الكود في المشاركة الاولى والمشكلة فيها :

انو عم صرح عن كلاس mychart في صفحة 1.jsp وبدي استخدم غرض من هذا الكلاس في صفحة تانية 2.jsp ؟؟؟؟؟؟؟

#7

لا يمكنك ذلك

استخدم java beans

او ضع الكلاس كما تريده في مجلد classes او حسب المشغل jsp الذي تملكه

و عندها تصبح طرقه متاحة للجميع

من خلال نظرتي السريعة للكود ما تفعله غير عملي و غير فعال ولا احد يفعل مثلما تفعل !!

Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why

#8

السلم عليكم أخي استعمل javabean كما قال رامي و من تم استدعيها في الصفحة jsp:usebean

image-27.gif

concepteur realisateur java j2ee

php5 and asp

#9
اقتباس
من خلال نظرتي السريعة للكود ما تفعله غير عملي و غير فعال ولا احد يفعل مثلما تفعل !!

بالراحة عليّ أخي رامي هذا أول موقع ب jsp .

ممكن شوية شرح على java bean مع مثال صغير

تم تعديل هذه المشاركة بواسطة hamzalion في 22 سبتمبر 2008 في 06:43

#10

الحمد لله اشتغلت على الطريقة التانية دون استخدام java bean

ان شاء مشان الفائدة راح ارفع الموقع يلي عم اعملوا قريبا وهو أول موقع الي jsp .

#11

بسم الله الرحمن الرحيم

بما أنني حاليا أدرس ولازلت مبتدأ بها ولكن من أجل الفائدة ونشر العلم فإليك طريقة إدراج كلاس للجافا في صفحة الجي اس بي

نفترض أن لديك الكلاس التالي :

import java.util.*;
import java.sql.*;
/**


public class Products 
{

	DbClass mydb = new DbClass();
	/** Creates a new instance of Products */
	public Products() {
	}

	public ArrayList getAllProducts()
	{
		Connection conn = null;
		PreparedStatement prpStmt = null;
		ResultSet rs = null;
		ArrayList productsList = new ArrayList();
		try
		{
		   mydb.setConnection();
		   conn = mydb.getConnection();
		   String sqlQuery = "SELECT * FROM PRODUCTS";
			prpStmt = conn.prepareStatement(sqlQuery);
			rs = prpStmt.executeQuery();

			while (rs.next()) {
				productsList.add(new Product(rs.getInt(1), rs.getString(2),rs.getString(3), rs.getString(4),rs.getDouble(5)));
			}
			rs.close();

			 prpStmt.close();
		}
		catch (Exception ex)
		{
			System.out.println(ex.getMessage());
		}

		mydb.closeConnection();
		return productsList;   
	}

	public ArrayList getAllProducts(int id)
	{
		Connection conn = null;
		PreparedStatement prpStmt = null;
		ResultSet rs = null;
		ArrayList productsList = new ArrayList();
		try
		{
		   mydb.setConnection();
		   conn = mydb.getConnection();
		   String sqlQuery = "SELECT * FROM PRODUCTS where productID=?";
			prpStmt = conn.prepareStatement(sqlQuery);
			prpStmt.setInt(1,id);
			rs = prpStmt.executeQuery();

			while (rs.next()) {
				productsList.add(new Product(rs.getInt(1), rs.getString(2),rs.getString(3), rs.getString(4),rs.getDouble(5)));
			}
			rs.close();

			 prpStmt.close();
		}
		catch (Exception ex)
		{
			System.out.println(ex.getMessage());
		}

		mydb.closeConnection();
		return productsList;   
	}


	public Product getProduct(int id)
	{
		Product prod = null;
		 Connection conn = null;
		PreparedStatement prpStmt = null;
		ResultSet rs = null;
		  String sqlQuery = "SELECT * FROM PRODUCTS WHERE productid = ?";
		  try
		  {
		   mydb.setConnection();
		   conn = mydb.getConnection();
		   prpStmt = conn.prepareStatement(sqlQuery);
		   prpStmt.setInt(1,id);
		   rs = prpStmt.executeQuery();
		   if (rs.next())
		   {
			prod = new Product(rs.getInt(1), rs.getString(2),rs.getString(3), rs.getString(4),rs.getDouble(5));   
		   }

		   rs.close();
			prpStmt.close();
		  }

		 catch (Exception ex)
		{
			System.out.println(ex.getMessage());
		} 

		mydb.closeConnection();
		return prod;

	}

	public int EditProduct(int id, double price)
	{
		int recsAffected = 0;
		Connection conn = null;
		PreparedStatement prpStmt = null;
		 String sqlQuery = "Update Products set productprice = ? where productid = ?";
		  try
		  {
		   mydb.setConnection();
		   conn = mydb.getConnection();
		   prpStmt = conn.prepareStatement(sqlQuery);
		   System.out.println(sqlQuery);
		   prpStmt.setInt(2,id);
		   prpStmt.setDouble(1,price);
		   recsAffected = prpStmt.executeUpdate();
		   System.out.println(recsAffected);
			prpStmt.close();
		  }
		   catch (Exception ex)
		{
			System.out.println(ex.getMessage());
		} 

		mydb.closeConnection();

		return recsAffected;

	}

	public int addNewProduct(String name, String desc, String fullDesc, double price )
	{
		 int recsAffected = 0;
		Connection conn = null;
		PreparedStatement prpStmt = null;
		 String sqlQuery = "Insert into Products(productName,productDescription,productLongDescription,productPrice)Values(?,?,?,?)";
		  try
		  {
		   mydb.setConnection();
		   conn = mydb.getConnection();
		   prpStmt = conn.prepareStatement(sqlQuery);
		   System.out.println(sqlQuery);
		   prpStmt.setString(1,name);
		   prpStmt.setString(2,desc);
		   prpStmt.setString(3,fullDesc);
		   prpStmt.setDouble(4,price);
		   recsAffected = prpStmt.executeUpdate();
		   System.out.println(recsAffected);
			prpStmt.close();
		  }
		   catch (Exception ex)
		{
			System.out.println(ex.getMessage());
		} 


		mydb.closeConnection();

		return recsAffected;

	}
}

باستخدام Bean يمكن استدعاه في الصفخة التي تريد كالتالي

<%@ page import="myclasses.Product" %>   
<%@ page import="java.util.*" %>
<jsp:useBean id="prods" scope="page" class="myclasses.Products" />
<%
ArrayList list = prods.getAllProducts();
boolean noneFound = false;
if (list.size()< 1)
	noneFound = true;

%>
#12

مشكور صديقي .

الصف يلي كتبتواحضرتك وين بدي اوضعه ........بالنهاية عندي مجلد موقع وين موقع الكلاس من الاعراب ضمن المجلد

تم تعديل هذه المشاركة بواسطة hamzalion في 27 أكتوبر 2008 في 07:05

#13

مشكور

تم تعديل هذه المشاركة بواسطة hamzalion في 27 أكتوبر 2008 في 07:06

مواضيع مشابهة