هذا المشروع اختصار لمشروع أخونا anas_aloklah لتسجيل الطلاب وفيه بعض التعديلات ، والاختصار ،
والمشرع بنيت له قاعدة بيانات بترميز : utf8mb4_unicode_ci واسمها : student_2
وفي القاعدة جدول بترميز : utf8mb4_unicode_ci واسم الجدول : std
وفي الجدول عامود واحد بترميز : utf8mb4_unicode_ci واسم العامود : Name وهو من النوع : varchar
الآن عند إرسال اسم باللغة الانجليزية يتم الإرسال ، ثم يتم عرض كل الأسماء بشكل صحيح ، ولكن لما أرسل اسم عربي تفشل عملية الإرسال ، علمًا أنني وضعت اسم بحروف عربية من خلال برنامج phpMyAdmin يعني ليس من المشروع وعند عرضه في المشروع يتم العرض بدون مشاكل ، ولكن المشكلة في الإرسال ، وهذا الكود :
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<jsp-config>
<jsp-property-group >
<url-pattern>*.jsp</url-pattern>
<page-encoding>UTF-8</page-encoding>
</jsp-property-group>
</jsp-config>
<servlet>
<servlet-name>addstudent</servlet-name>
<servlet-class>Students.addstudent</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>addstudent</servlet-name>
<url-pattern>/addstudent</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>addstudent.jsp</welcome-file>
</welcome-file-list>
</web-app>
addstudent.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>إضافة طالب</title>
</head>
<body>
<form name=f action="addstudent" >
Name :
<input type=text name=ftext id=fn>
<font id=fid class=valid1>*</font><br>
<input type=button value=validate onclick="validateAll()">
<input type=submit onclick="return validateAll()">
</form>
</body>
</html>
ShowAll.jsp :
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="Students.StudentInfo"%>
<%@page import="java.util.ArrayList"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="shortcut icon" href="cd_dvd.png">
</head>
<body>
<%
ArrayList allstd = StudentInfo.getAll();
for (int i = 0; i < allstd.size(); i++) {
StudentInfo st = (StudentInfo) allstd.get(i);
String name = st.getName();
%>
<form action="updataAndDelete.jsp">
<table border="0" >
<tr>
<td> <%= name%></td>
</tr>
</table>
</form>
<% }%>
</body>
</html>
DBConnect.java
package Students;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DBConnect {
private String MySqlURL = "jdbc:mysql://";
private Connection connection;
private String hostName = "localhost:3306";
private String dbName = "student_2";
private String user = "root";
private String password = "";
private Statement st;
public DBConnect() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
throw new SecurityException("class not found. " + ex.toString());
}
try {
connection = DriverManager.getConnection(MySqlURL + hostName + "/"
+ dbName + "?user=" + user + "&password=" + password);
connection.setAutoCommit(true);
st = connection.createStatement();
st.executeQuery("SET character_set_client=utf8");
st.executeQuery("SET character_set_connection=utf8");
st.executeQuery("SET character_set_database=utf8");
st.executeQuery("SET character_set_results=utf8");
st.executeQuery("SET character_set_server=utf8");
} catch (Exception ex) {
throw new SecurityException("Cannot connect to MySql database. "
+ ex.toString());
}
}
public DBConnect(String hostName, String dbName, String user, String password) throws SecurityException {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
throw new SecurityException("class not found. " + ex.toString());
}
try {
connection = DriverManager.getConnection(MySqlURL + hostName + "/"
+ dbName + "?user=" + user + "&password=" + password);
connection.setAutoCommit(true);
st = connection.createStatement();
st.executeQuery("SET character_set_client=utf8");
st.executeQuery("SET character_set_connection=utf8");
st.executeQuery("SET character_set_database=utf8");
st.executeQuery("SET character_set_results=utf8");
st.executeQuery("SET character_set_server=utf8");
} catch (Exception ex) {
throw new SecurityException("Cannot connect to MySql database. "
+ ex.toString());
}
}
public ResultSet excuteSelect(String selectStat) throws Exception {
return st.executeQuery(selectStat);
}
public void addSTD(String dmlStat) {
try {
DBConnect db = new DBConnect();
st.executeUpdate("insert into std values ( '" +dmlStat+ "' )");
db.close();
} catch (Exception ex) {
System.out.print(ex);
}
}
public void excuteDml(String dmlStat) {
try {
st.executeUpdate(dmlStat);
} catch (SQLException ex) {
Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void close() {
try {
st.close();
connection.close();
} catch (Exception ex) {
throw new SecurityException("can not close the connection. " + ex.toString());
}
}
}
addstudent.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Students;
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 addstudent extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
String name=request.getParameter("ftext");
StudentInfo st =new StudentInfo(name);
st.addSTD();
response.sendRedirect("http://localhost:8080/Students_2/ShowAll.jsp");
}
@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";
}
}
StudentInfo.java
package Students;
import java.sql.*;
import java.util.ArrayList;
public class StudentInfo {
private String Name;
public StudentInfo() {
}
public StudentInfo( String Name) {
this.setName(Name);
}
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public void addSTD() {
try {
DBConnect db = new DBConnect();
db.excuteDml("insert into std values ( '" + this.Name + "' )");
db.close();
} catch (Exception ex) {
System.out.print(ex);
}
}
public static ArrayList getAll() {
DBConnect db = new DBConnect();
ArrayList allStd = new ArrayList();
try {
ResultSet rs = db.excuteSelect("SELECT * FROM std");
int i = 0;
while (rs.next()) {
StudentInfo st = new StudentInfo();
st.Name = rs.getString("Name");
i++;
allStd.add(st);
}
db.close();
} catch (Exception ex) {
System.out.print(ex);
}
return allStd;
}
}
يرفع من قيمة الموضوع واهمية الرد