我想将项目切换到基于Spring的项目。
我没有找到任何关于如何将传统的web.xml
项目的servlet迁移到Spring引导的指南。
以下是我的servlet.java
供您参考。
@WebServlet(name = "Servlet")
public class Servlet extends HttpServlet
{
//final static Logger logger=Logger.getLogger("Servlet.class");
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
variables.scrollflag = 1;
if (request.getParameter("ins") != null) {
variables.cruiseflag=0;
variables.flag6 = 0;
variables.flag9 = 0;
variables.labelflag = 0;
variables.displayflag=0;
String n = request.getParameter("inserttextbox1");
StringBuilder sb = new StringBuilder(n);
for (int index = 0; index < sb.length(); index++) {
char c = sb.charAt(index);
if (Character.isLowerCase(c)) {
sb.setCharAt(index, Character.toUpperCase(c));
}
}
PrintWriter o = response.getWriter();
o.print(sb.toString());
String n2 = request.getParameter("inserttextbox2");
StringBuilder sb2 = new StringBuilder(n2);
for (int index = 0; index < sb2.length(); index++) {
char c = sb2.charAt(index);
if (Character.isLowerCase(c)) {
sb2.setCharAt(index, Character.toUpperCase(c));
}
}
String n3 = request.getParameter("inserttextbox3");
StringBuilder sb3 = new StringBuilder(n3);
for (int index = 0; index < sb3.length(); index++) {
char c = sb3.charAt(index);
if (Character.isLowerCase(c)) {
sb3.setCharAt(index, Character.toUpperCase(c));
}
}
Properties properties = new Properties();
try {
InputStream in = this.getClass().getResourceAsStream("../../main/resources/database.properties");
properties.load(in);
} catch (Exception e) {
e.printStackTrace();
// logger.severe("SQL exception. Could not load properties file.");
}
Connection conn = null;
Context ctx = null;
try {
ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/bhdatasource");
conn=ds.getConnection();
} catch (Exception c) {
c.printStackTrace();
// logger.severe("SQL exception. Connection URL, User Name or Password is incorrect. Please check the server.xml file.");
}
try {
List<beans> list234 = new ArrayList<>();
PreparedStatement pstmt = conn.prepareStatement(properties.getProperty("ins_search"));
pstmt.setString(1, sb.toString());
pstmt.setString(2, sb2.toString());
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
beans e1 = new beans();
e1.setId(rs.getInt(1));
e1.setpccma(rs.getString(2));
e1.setpccal(rs.getString(3));
e1.setcname(rs.getString(4));
list234.add(e1);
request.setAttribute("searchlist2", list234);
String msg = "Record already exists";
request.setAttribute("msg", msg);
conn.close();
ctx.close();
RequestDispatcher dis = request.getRequestDispatcher("/pages/insertpcc.jsp?page=1");
dis.forward(request, response);
return;
}
List<beans> list = new ArrayList<>();
PreparedStatement stmt = conn.prepareStatement(properties.getProperty("insert"));
stmt.setString(1, sb.toString());
stmt.setString(2, sb2.toString());
stmt.setString(3, sb3.toString());
stmt.executeUpdate();
PreparedStatement stmt1 = conn.prepareStatement(properties.getProperty("show_insert"));
stmt1.setString(1, sb2.toString());
stmt1.setString(2, sb.toString());
stmt1.setString(3, sb3.toString());
ResultSet rs1 = stmt1.executeQuery();
while (rs1.next()) {
beans e1 = new beans();
e1.setId(rs1.getInt(1));
e1.setpccma(rs1.getString(2));
e1.setpccal(rs1.getString(3));
e1.setcname(rs1.getString(4));
list.add(e1);
}
request.setAttribute("searchlist2", list);
String msg = "Row inserted. Flag is set to Y";
request.setAttribute("msg", msg);
String yflag = "Y";
PreparedStatement pst = conn.prepareStatement(properties.getProperty("updflag"));
pst.setString(1, yflag);
pst.executeUpdate();
conn.commit();
conn.close();
ctx.close();
RequestDispatcher dis = request.getRequestDispatcher("/pages/insertpcc.jsp?page=1");
dis.forward(request, response);
return;
} catch (SQLException e) {
e.printStackTrace();
// logger.severe("SQL exception");
}
catch (NamingException e) {
// logger.severe("Exception in closing Context");
}
}
if (request.getParameter("two") != null || request.getParameter("search") != null) {
variables.labelflag = 0;
variables.flag6 = 0;
variables.flag9 = 0;
variables.cruiseflag=0;
variables.displayflag=0;
String n = request.getParameter("textbox1");
StringBuilder sb = new StringBuilder(n);
for (int index = 0; index < sb.length(); index++) {
char c = sb.charAt(index);
if (Character.isLowerCase(c)) {
sb.setCharAt(index, Character.toUpperCase(c));
}
}
PrintWriter o = response.getWriter();
o.print(sb.toString());
String n2 = request.getParameter("textbox2");
StringBuilder sb2 = new StringBuilder(n2);
for (int index = 0; index < sb2.length(); index++) {
char c = sb2.charAt(index);
if (Character.isLowerCase(c)) {
sb2.setCharAt(index, Character.toUpperCase(c));
}
}
Properties properties = new Properties();
try {
InputStream in = this.getClass().getResourceAsStream("../../main/resources/database.properties");
properties.load(in);
} catch (Exception e) {
e.printStackTrace();
// logger.severe("SQL exception. Could not load properties file.");
}
Connection conn = null;
Context ctx=null;
try {
ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/bhdatasource");
conn=ds.getConnection();
} catch (Exception c) {
c.printStackTrace();
// logger.severe("SQL exception. Connection URL, User Name or Password is incorrect. Please check the server.xml file.");
}
List<beans> list = new ArrayList<>();
try {
PreparedStatement stmt = conn.prepareStatement(properties.getProperty("search"));
PreparedStatement stmt2 = conn.prepareStatement(properties.getProperty("search2"));
stmt.setString(1, sb2.toString());
stmt2.setString(1, sb2.toString());
stmt2.setString(2, sb.toString());
ResultSet rs;
if (sb.toString().equals("")) {
rs = stmt.executeQuery();
} else {
rs = stmt2.executeQuery();
}
if (!(rs.isBeforeFirst())) {
variables.labelflag = 1;
variables.flag6 = 1;
String msg = "No records found!";
request.setAttribute("msg", msg);
conn.close();
ctx.close();
RequestDispatcher dis = request.getRequestDispatcher("/pages/searchpcc.jsp?page=1");
dis.forward(request, response);
return;
}
while (rs.next()) {
beans e = new beans();
e.setId(rs.getInt(1));
e.setpccma(rs.getString(2));
e.setpccal(rs.getString(3));
e.setcname(rs.getString(4));
list.add(e);
}
request.setAttribute("searchlist", list);
conn.close();
variables.y = 1;
RequestDispatcher dis1 = request.getRequestDispatcher("/pages/searchpcc.jsp?page=1");
dis1.forward(request, response);
return;
} catch (SQLException e) {
e.printStackTrace();
//logger.severe("SQL exception");
}
catch (NamingException e){
e.printStackTrace();
//logger.severe("Exception in closing context");
}
}
if (request.getParameter("upd") != null) {
variables.cruiseflag=0;
variables.flag6 = 0;
variables.ra = 0;
variables.flag9 = 1;
variables.displayflag=0;
variables.labelflag = 0;
String n = request.getParameter("updatetextbox1");
StringBuilder sb = new StringBuilder(n);
for (int index = 0; index < sb.length(); index++) {
char c = sb.charAt(index);
if (Character.isLowerCase(c)) {
sb.setCharAt(index, Character.toUpperCase(c));
}
}
PrintWriter o = response.getWriter();
o.print(sb.toString());
String n2 = request.getParameter("updatetextbox3");
StringBuilder sb2 = new StringBuilder(n2);
for (int index = 0; index < sb2.length(); index++) {
char c = sb2.charAt(index);
if (Character.isLowerCase(c)) {
sb2.setCharAt(index, Character.toUpperCase(c));
}
}
String n3 = request.getParameter("updatetextbox5");
StringBuilder sb3 = new StringBuilder(n3);
for (int index = 0; index < sb3.length(); index++) {
char c = sb3.charAt(index);
if (Character.isLowerCase(c)) {
sb3.setCharAt(index, Character.toUpperCase(c));
}
}
String n21 = request.getParameter("updatetextbox2");
StringBuilder sb21 = new StringBuilder(n21);
for (int index = 0; index < sb21.length(); index++) {
char c = sb21.charAt(index);
if (Character.isLowerCase(c)) {
sb21.setCharAt(index, Character.toUpperCase(c));
}
}
String n22 = request.getParameter("updatetextbox4");
StringBuilder sb22 = new StringBuilder(n22);
for (int index = 0; index < sb22.length(); index++) {
char c = sb22.charAt(index);
if (Character.isLowerCase(c)) {
sb22.setCharAt(index, Character.toUpperCase(c));
}
}
String n23 = request.getParameter("updatetextbox6");
StringBuilder sb23 = new StringBuilder(n23);
for (int index = 0; index < sb23.length(); index++) {
char c = sb23.charAt(index);
if (Character.isLowerCase(c)) {
sb23.setCharAt(index, Character.toUpperCase(c));
}
}
Properties properties = new Properties();
try {
InputStream in = this.getClass().getResourceAsStream("../../main/resources/database.properties");
properties.load(in);
} catch (Exception e) {
e.printStackTrace();
// logger.severe("SQL exception. Could not load properties file.");
}
Connection conn = null;
Context ctx=null;
try {
ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/bhdatasource");
conn=ds.getConnection();
} catch (Exception c) {
c.printStackTrace();
//logger.severe("SQL exception. Connection URL, User Name or Password is incorrect. Please check the server.xml file.");
}
try {
List<beans> list1 = new ArrayList<>();
List<beans> list23 = new ArrayList<>();
List<beans> list233 = new ArrayList<>();
PreparedStatement stmt2 = conn.prepareStatement(properties.getProperty("updatevalid"));
PreparedStatement stmt4 = conn.prepareStatement(properties.getProperty("updatevald2"));
stmt2.setString(1, sb2.toString());
stmt2.setString(2, sb.toString());
stmt4.setString(1, sb2.toString());
stmt4.setString(2, sb.toString());
stmt4.setString(3, sb3.toString());
ResultSet rs9;
if (sb3.toString().equals("")) {
rs9 = stmt2.executeQuery();
} else {
rs9 = stmt4.executeQuery();
}
if (!(rs9.isBeforeFirst())) {
String msg = "No records found!";
request.setAttribute("msg", msg);
conn.close();
ctx.close();
RequestDispatcher dis = request.getRequestDispatcher("/pages/updatepcc.jsp?page=1");
dis.forward(request, response);
return;
}
while (rs9.next()) {
beans e = new beans();
e.setId(rs9.getInt(1));
e.setpccma(rs9.getString(2));
e.setpccal(rs9.getString(3));
e.setcname(rs9.getString(4));
list1.add(e);
}
request.setAttribute("searchlist31", list1);
PreparedStatement pstmt = conn.prepareStatement(properties.getProperty("updatedup"));
pstmt.setString(1, sb21.toString());
pstmt.setString(2, sb22.toString());
pstmt.setString(3, sb23.toString());
ResultSet rs11 = pstmt.executeQuery();
if (rs11.next()) {
beans e1 = new beans();
e1.setId(rs11.getInt(1));
e1.setpccma(rs11.getString(2));
e1.setpccal(rs11.getString(3));
e1.setcname(rs11.getString(4));
list233.add(e1);
request.setAttribute("searchlist31", list233);
String msg = "No records updated since new values already exists";
request.setAttribute("msg", msg);
conn.close();
ctx.close();
RequestDispatcher dis = request.getRequestDispatcher("/pages/updatepcc.jsp?page=1");
dis.forward(request, response);
} else {
PreparedStatement stmt, stmt9;
stmt = conn.prepareStatement(properties.getProperty("update1"));
stmt.setString(1, sb21.toString());
stmt.setString(2, sb22.toString());
stmt.setString(3, sb23.toString());
stmt.setString(4, sb2.toString());
stmt.setString(5, sb.toString());
stmt9 = conn.prepareStatement(properties.getProperty("update2"));
stmt9.setString(1, sb21.toString());
stmt9.setString(2, sb22.toString());
stmt9.setString(3, sb23.toString());
stmt9.setString(4, sb2.toString());
stmt9.setString(5, sb.toString());
stmt9.setString(6, sb3.toString());
int no_of_records;
if (sb3.toString().equals("")) {
no_of_records = stmt.executeUpdate();
} else {
no_of_records = stmt9.executeUpdate();
}
PreparedStatement stmt11 = conn.prepareStatement(properties.getProperty("updatedisp"));
stmt11.setString(1, sb22.toString());
stmt11.setString(2, sb21.toString());
stmt11.setString(3, sb23.toString());
ResultSet rs1 = stmt11.executeQuery();
while (rs1.next()) {
beans e1 = new beans();
e1.setId(rs1.getInt(1));
e1.setpccma(rs1.getString(2));
e1.setpccal(rs1.getString(3));
e1.setcname(rs1.getString(4));
list23.add(e1);
}
request.setAttribute("searchlist3", list23);
String msg = " row updated. Flag is set to Y";
request.setAttribute("msg", no_of_records + msg);
String yflag1 = "Y";
PreparedStatement pst = conn.prepareStatement(properties.getProperty("updflag1"));
pst.setString(1, yflag1);
pst.executeUpdate();
conn.commit();
conn.close();
ctx.close();
RequestDispatcher dis = request.getRequestDispatcher("/pages/updatepcc.jsp?page=1");
dis.forward(request, response);
}
} catch (SQLException e) {
e.printStackTrace();
// logger.severe("SQL exception");
}
catch (NamingException e){
e.printStackTrace();
// logger.severe("Exception in closing context");
}
}
if (request.getParameter("submit") != null) {
variables.displayflag=1;
String[] words0 = request.getParameterValues("cruiselinedropdown");
String[] words = request.getParameterValues("descriptextbox");
String[] words2 = request.getParameterValues("starttimetextbox");
String[] words3 = request.getParameterValues("endtimetextbox");
Properties properties = new Properties();
try {
InputStream in = this.getClass().getResourceAsStream("../../main/resources/database.properties");
properties.load(in);
} catch (Exception e) {
e.printStackTrace();
// logger.severe("SQL exception. Could not load properties file.");
}
Connection conn = null;
Context ctx=null;
try {
ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/bhdatasource");
conn=ds.getConnection();
} catch (Exception c) {
c.printStackTrace();
// logger.severe("SQL exception. Connection URL, User Name or Password is incorrect. Please check the server.xml file.");
}
int cruisemaxid=0;
try {
PreparedStatement pstmt2 = conn.prepareStatement(properties.getProperty("maxid"));
ResultSet rsmaxid2 = pstmt2.executeQuery();
if(rsmaxid2.next()){
cruisemaxid=rsmaxid2.getInt("idmax");
}
PreparedStatement pstmt = conn.prepareStatement(properties.getProperty("cruiseinsert"));
for(int i=0;i<words2.length;i++)
{
pstmt.setInt(1,cruisemaxid+i+1);
pstmt.setString(2,words0[i]);
pstmt.setString(3,words2[i]);
pstmt.setString(4,words3[i]);
pstmt.setString(5,words[i]);
pstmt.addBatch();
}
int[] result=pstmt.executeBatch();
String cruiserowsinserted = "No. of rows inserted : "+result.length;
request.setAttribute("cruiserowsinserted", cruiserowsinserted);
conn.commit();
conn.close();
ctx.close();
doGet(request,response);
} catch (SQLException e) {
e.printStackTrace();
// logger.severe("SQL exception");
}
catch (NamingException e){
e.printStackTrace();
// logger.severe("Exception in closing context");
}
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(variables.displayflag==1)variables.displayflag=1;
Properties properties = new Properties();
try {
InputStream in = this.getClass().getResourceAsStream("../../main/resources/database.properties");
properties.load(in);
} catch (Exception e) {
e.printStackTrace();
}
Connection conn = null;
Context ctx=null;
try {
ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/bhdatasource");
conn=ds.getConnection();
} catch (Exception c) {
c.printStackTrace();
// logger.severe("SQL exception. Connection URL, User Name or Password is incorrect. Please check the server.xml file.");
}
List<beans> cruiselinelist3 = new ArrayList<>();
List<beans> cruiselinelist = new ArrayList<>();
try {
PreparedStatement ps3=conn.prepareStatement(properties.getProperty("crzcode"));
ResultSet rscruise3 = ps3.executeQuery();
while (rscruise3.next()) {
beans ecruise3 = new beans();
ecruise3.setCruisecode(rscruise3.getString("CRZCODE"));
cruiselinelist3.add(ecruise3);
}
request.setAttribute("cruiselist3",cruiselinelist3);
PreparedStatement ps=conn.prepareStatement(properties.getProperty("cruiselines"));
ResultSet rscruise = ps.executeQuery();
while (rscruise.next()) {
beans ecruise = new beans();
ecruise.setCruiseline(rscruise.getString(1));
cruiselinelist.add(ecruise);
}
request.setAttribute("cruiselist",cruiselinelist);
conn.close();
ctx.close();
RequestDispatcher dis = request.getRequestDispatcher("/pages/cruisedowntime.jsp?page=1");
dis.forward(request, response);
}
catch (SQLException e) {
e.printStackTrace();
// logger.severe("SQL exception");
}
catch (NamingException e){
e.printStackTrace();
// logger.severe("Exception in closing context");
}
}
}}