我正在设置一个Web应用程序,以通过JSP在DB中存储多语言数据,我试图将字符集更改为UTF-8,这是行不通的。我的表格仅在正确的配置中(nvarchar),尽管没有通过Internet获得相关示例,但我还是编程的初学者。谁能帮我解决这个问题。预先感谢
我的控制器代码:
package com.suth.DbConnectiviti;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginController extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("fullname");
String userName = request.getParameter("userName");
String pass = request.getParameter("pass");
String addr = request.getParameter("address");
String age = request.getParameter("age");
String qual = request.getParameter("qual");
String percent = request.getParameter("percentage");
String year = request.getParameter("yop");
if(name.isEmpty()||addr.isEmpty()||age.isEmpty()||qual.isEmpty()||percent.isEmpty()||year.isEmpty())
{
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
out.println("<font color=red>Please fill all the fields</font>");
rd.include(request, response);
}
else
{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// loads driver
Connection c = DriverManager.getConnection("jdbc:sqlserver://****;databaseName=***?useUnicode=yes&characterEncoding=UTF-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull", "****", "****!");
PreparedStatement ps = c.prepareStatement("insert into student1 values(?,?,?,?,?,?,?,?)");
ps.setString(1, name);
ps.setString(2, userName);
ps.setString(3, pass);
ps.setString(4, addr);
ps.setInt(5, Integer.parseInt(age));
ps.setString(6, qual);
ps.setString(7, percent);
ps.setString(8, year);
ps.executeUpdate(); // execute it on test database
System.out.println("successfuly inserted");
ps.close();
c.close();
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
RequestDispatcher rd = request.getRequestDispatcher("error.html");
rd.forward(request, response);
}
} }
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Guru Registration Form</title>
</head>
<body>
<h1>Guru Register Form</h1>
<form action="LoginController" method="post">
<table style="with: 50%">
<tr>
<td>First Name</td>
<td><input type="text" name="fullname" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="userName" /></td>
</tr>
<tr>
<td>UserName</td>
<td><input type="text" name="pass" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="address" /></td>
</tr>
<tr>
<td>Address</td>
<td><input type="text" name="age" /></td>
</tr>
<tr>
<td>Contact No</td>
<td><input type="text" name="qual" /></td>
</tr>
<tr>
<td>Percentage</td>
<td><input type="text" name="percentage" /></td>
</tr>
<tr>
<td>Year Passed</td>
<td><input type="text" name="yop" /></td>
</tr></table>
<input type="submit" value="Submit" /></form>
`