# Servletproblem.brauche dringend einen Rat



## angela (27. Nov 2007)

Hallo ...habe ein Problem beim folgenden Text:


```
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.*;
import java.util.*;
import java.sql.*;
import java.lang.String.*;

public class searchservlet extends HttpServlet 
{
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
    {
		resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();		
		String search = req.getParameter("search");	  
        out.println("<html>");
        out.println("<body>");
        out.println("<head>");
        out.println("<title>Hello World!</title>");
        out.println("</head>");
        out.println("<body bgcolor='white' text='black'>"); 
		//String sentance = "Sovereign Diamonds ";
		String sentance = " Threes stones diamonds   ";				
		sentance = sentance.trim();
		String wordis = search;
		String wordis2 = " ";
		StringTokenizer st = new StringTokenizer(sentance);
		String wordnow= " ";	
		int wlen;
		String lastchar;
		
		while (st.hasMoreTokens()) 
		{
			wordnow = st.nextToken();
			wordnow = wordnow.trim();
			out.println("
wordnow"+wordnow);
			wlen = wordnow.length();
			wlen = wlen-1;
			out.println("
wlen"+wlen);
			lastchar = wordnow.charAt(wlen);
							
			///*
			if(lastchar == "s")
			{
				out.println("
S FOUND");
			}
			else
			{
				out.println("
S NOT FOUND");
			}
			//*/
			//out.println("
lastchar"+lastchar);
		}		
   		//Connection=====================================================
		java.util.Properties props = new java.util.Properties();
		props.put("user", " ");
	    props.put("password", " ");	
		String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
		String mydsn = "jdbc:odbc:shop";
		String wordxx;
		query(driverName,mydsn,props,out,resp,sentance);		
		//End Connection -----------------------------------------------
        out.println("</body>");
        out.println("</html>");
    }
		
	public void init(ServletConfig cfg) throws ServletException
    {
      super.init(cfg);
    }

  	public void destroy()
    {
      super.destroy();
    }
	
	private void query (String driverName , String connectionURL , java.util.Properties props ,java.io.PrintWriter out, HttpServletResponse resp, String sentance)
    {
		
		boolean rc = true;
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
		
		//int charlen 
		//char lastchar = 
		
		String query = "SELECT * FROM keywords WHERE mykeywords like '%"+sentance+"%'";
		out.println(query);
		String keyword = "";
		try 
		{
	        Class.forName(driverName).newInstance();
        	con = DriverManager.getConnection(connectionURL, props);
        	stmt = con.createStatement();
        	rs = stmt.executeQuery(query);
        	out.println(sentance);	
			while (rs.next()) 
			{							
					keyword=rs.getString(3);
					String ProdID=rs.getString(2);
					out.println(" 

 ");	
					out.println(ProdID);
					out.println(keyword);					
					//username=rs.getString(2);	
					//validlogin = true;			
			}
			
			
				/*			
						
					out.println("
");		
					while (st.hasMoreTokens()) 
				 	{
						wordnow = st.nextToken();
						if (wordnow.equalsIgnoreCase(wordis))
						{
							out.println("Found It");	
						}		
						else
						{
							int wordlen = wordnow.length();				
							if (wordlen>1)
							{
								wordlen = wordlen-1;
							}				
							String wordnow2 = wordnow.substring(0, wordlen);	
							if (wordnow2.equalsIgnoreCase(wordis))
							{
								out.println("Found It");	
							}
							else
							{
								int wordislen = wordis.length();
								if (wordislen>1)
								{
									wordislen = wordislen-1;
									wordis2 = wordis.substring(0, wordislen);	
								}
								if (wordnow.equalsIgnoreCase(wordis2))
								{
									out.println("Found It");	
								}					
							}
						}	
			     	}
					*/
			
      	}
      	catch (Exception ex) 
		{
        	out.println("Exception Could not Connect to DB!");
      	}
      	finally 
		{
        	try 
			{
          		if (rs != null) 
				{
            		rs.close();
          		}
          		if (stmt != null) 
				{
            		stmt.close();
          		}
          		if (con != null) 
				{
            		con.close();
          		}
        	}
        	catch (Exception ex) 
			{
          		// Do Nothing here
        	}
      }
		
	}
	
	
}
```

Fehlermeldung bei :  lastchar = wordnow.charAt(wlen);


Kann mir jemand weiterhelfen...weiß mir keinen Rat mehr.....

Vielen vielen Dank ...)))


----------



## Kim Stebel (27. Nov 2007)

lastChar sollte vom typ char sein


----------



## angela (27. Nov 2007)

Danke erstmal...aber wenn ich die Zeile 31 : char lastchar:
schreibe bekomme ich in Zeile 44 wieder eine Fehlermeldung...neues Problem???...
Angela


----------



## SlaterB (27. Nov 2007)

if(lastchar == 's')


----------



## angela (27. Nov 2007)

oh...super...Vielen Dank für die schnelle Hilfe!!!...:O)...jetzt wurde eine .class Datei erstellt...
Angela


----------

